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