Hi there,
has anyone here had a chance to create a Python dash app using data from AWS Athena? I am trying to do so and having difficulty automatically querying structured data from my data lake (AWS Athena) via Python.
Specifically, I have been creating a real-time report about information of each user, and as the user inputs their own ID to dcc.Input, then all relevant information about that user will render. Here the thing is, I must query all user IDs and their related data first, then my app can work well, but there are a very large number of users there => my app starts extremely slowly.
Now, I’ve been trying placing my Python queries to under callback, meaning that I want whenever the user inputs their own ID to dcc.Input, it will trigger my query codes for only that user ID. The below is my codes for this but it does not work:
#essential libraries
import pandas as pd
import numpy as npimport plotly.express as px
import dash
from dash import dcc, html
from dash import dash_table as dt
from dash.dependencies import Input, Outputfrom pyathena import connect
#DASH APP
app = dash.Dash(name)#--------------------------------------------------
app.layout = html.Div(children=[
add_logo(),
*make_break(2),html.Div ([ #Input Box for user ID and Dropdown for each respective profile name html.Div ([ html.Label('user ID'), *make_break(1), dcc.Input(id="input_user_id", type="text", placeholder="Input your ID here và Enter!", debounce=True, #True = enter or tab to execute this input box style={'margin':'auto', 'display':'inline-block', "width":'70%','height':'29px', 'font-size' :'115%'}), html.Div(id="output"), ], style={'padding': 10, 'flex': 1}),…
],style={‘text-align’:‘center’, ‘width’:‘100%’,‘background-image’:‘url(“/assets/background-MS.jpg”)’})
#--------------------------------------------------
@app.callback(
Output(‘output’, ‘children’),
Input(‘input_user_id’, ‘value’),
)
def update_query_output(ID):
#query from AWS Athena
ms=pd.read_sql(f"“”
SELECT h.*
FROM
(
SELECT
CAST(date_format(date_parse( SUBSTR(cast(“ts” as varchar(8)), 1, 8), ‘%Y%m%d’ ), ‘%Y-%m-%d’) AS DATE) as “date”,
“ts”,
“profile.identity” as “id”,
“eventname”,
“eventprops.lesson_name”,
“eventprops.completed”,
“eventprops.course_type”,
FROM clevertap_db_user.my_db
) h
WHERE h.ID == ‘{ID}’
“”", con = conn)@app.callback(
…
)
…
#--------------------------------------------------
if name == ‘main’:
app.run_server(debug=False)
Then, running the above codes will result in an error saying that name ‘ms’ is not defined