Py.Cafe

jackparmer/

duckdb-demo

Interactive Data Visualization with DuckDB and Dash

DocsPricing
  • app.py
  • requirements.txt
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import dash
import dash_html_components as html
import dash_core_components as dcc
import plotly.express as px
from dash.dependencies import Input, Output
import duckdb
import pandas as pd

# Initialize the Dash app
app = dash.Dash(__name__)

# Create a DuckDB connection and load some sample data
conn = duckdb.connect(database=':memory:')

# Create a sample dataframe
df = pd.DataFrame({
    'name': ['Alice', 'Bob', 'Charlie', 'David', 'Eve'],
    'age': [24, 27, 22, 32, 29],
    'city': ['New York', 'Los Angeles', 'Chicago', 'Houston', 'Phoenix']
})

# Load the dataframe into DuckDB
conn.execute('CREATE TABLE people AS SELECT * FROM df')

# Define the layout of the Dash app
app.layout = html.Div([
    html.H1("DuckDB Demo", style={'textAlign': 'center'}),
    html.Div([
        html.Label('Select Query:'),
        dcc.Dropdown(
            id='query-dropdown',
            options=[
                {'label': 'Select All', 'value': 'SELECT * FROM people'},
                {'label': 'Filter by Age > 25', 'value': 'SELECT * FROM people WHERE age > 25'},
                {'label': 'Group by City', 'value': 'SELECT city, COUNT(*) as count FROM people GROUP BY city'}
            ],
            value='SELECT * FROM people'
        ),
    ], style={'width': '50%', 'margin': 'auto'}),
    dcc.Graph(id='results-graph')
])

# Callback to update the graph based on the selected query
@app.callback(
    Output('results-graph', 'figure'),
    [Input('query-dropdown', 'value')]
)
def update_graph(query):
    # Execute the selected query
    df_result = conn.execute(query).df()

    # Determine the type of plot based on the query
    if 'count' in df_result.columns:
        fig = px.bar(df_result, x='city', y='count', title='Count by City')
    else:
        fig = px.scatter(df_result, x='name', y='age', color='city', title='People Data')

    return fig

# Run the app
if __name__ == '__main__':
    app.run_server(debug=True)