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)