Python Plotly Dash:如何为不同的用户展示不同的数据?
Python Plotly Dash: How to display different data for the different users?
我使用 PostgreSQL 查询作为 Plotly Dash 仪表板的数据源。
在查询开始时,我通过 v_user 参数定义 UserID 并仅为该用户获取数据,并根据这些数据构建一个仪表板。
import psycopg2
import pandas as pd
import plotly.express as px
import dash
import dash_core_components as dcc
import dash_html_components as html
v_user=111
conn = psycopg2.connect(host="xxxx", port = 5432, database="xxxxx", user="xxxxx", password="xxxxx")
statment= f""" select month, count(id) as sales from public.orders where user_profile_id={v_user} group by 1"""
df_orders= pd.read_sql_query(statment ,con=conn)
df_orders
app = dash.Dash()
fig = px.bar(df_orders, x="month", y="sales")
app.layout = html.Div([
html.H1('Sales by Users'),
html.Div([dcc.Graph(figure=fig)])
])
if __name__ == '__main__':
app.run_server(debug=True, use_reloader=False)
目标是只显示每个用户的数据,这意味着我需要在 SQL 查询之前更改 v_user 参数。
这意味着用户“111”只能看到它的数据,用户“222”只能看到它的数据。
怎么做?
或者也许还有另一种方法可以为每个用户过滤和显示不同的数据??
在破折号中你有什么是调用回调函数,它获取输入和输出参数
在您的情况下,您需要添加一种方法 select 用户 ID 并将其传递给回调函数
import psycopg2
import pandas as pd
import plotly.express as px
import dash
import dash_core_components as dcc
import dash_html_components as html
v_user=111
conn = psycopg2.connect(host="xxxx", port = 5432, database="xxxxx", user="xxxxx", password="xxxxx")
statment= f""" select month, count(id) as sales from public.orders where user_profile_id={v_user} group by 1"""
df_orders= pd.read_sql_query(statment ,con=conn)
df_orders
app = dash.Dash()
fig = px.bar(df_orders, x="month", y="sales")
app.layout = html.Div([
html.H1('Sales by Users'),
html.Div([dcc.Graph(id='graph_1', figure=fig)])
])
if __name__ == '__main__':
app.run_server(debug=True, use_reloader=False)
@app.callback(
Output(component_id="graph_1", component_property="figure"),
[Input(component_id="{add the component that hold the user id}", component_property='value')]
)
def update_dates(user_id):
conn = psycopg2.connect(host="xxxx", port = 5432, database="xxxxx",
user="xxxxx", password="xxxxx")
statment= f""" select month, count(id) as sales from public.orders where
user_profile_id={user_id} group by 1"""
df_orders= pd.read_sql_query(statment ,con=conn)
return px.bar(df_orders, x="month", y="sales")
您需要了解您的用户 ID 在哪里以及何时更改
我使用 PostgreSQL 查询作为 Plotly Dash 仪表板的数据源。 在查询开始时,我通过 v_user 参数定义 UserID 并仅为该用户获取数据,并根据这些数据构建一个仪表板。
import psycopg2
import pandas as pd
import plotly.express as px
import dash
import dash_core_components as dcc
import dash_html_components as html
v_user=111
conn = psycopg2.connect(host="xxxx", port = 5432, database="xxxxx", user="xxxxx", password="xxxxx")
statment= f""" select month, count(id) as sales from public.orders where user_profile_id={v_user} group by 1"""
df_orders= pd.read_sql_query(statment ,con=conn)
df_orders
app = dash.Dash()
fig = px.bar(df_orders, x="month", y="sales")
app.layout = html.Div([
html.H1('Sales by Users'),
html.Div([dcc.Graph(figure=fig)])
])
if __name__ == '__main__':
app.run_server(debug=True, use_reloader=False)
目标是只显示每个用户的数据,这意味着我需要在 SQL 查询之前更改 v_user 参数。 这意味着用户“111”只能看到它的数据,用户“222”只能看到它的数据。 怎么做? 或者也许还有另一种方法可以为每个用户过滤和显示不同的数据??
在破折号中你有什么是调用回调函数,它获取输入和输出参数 在您的情况下,您需要添加一种方法 select 用户 ID 并将其传递给回调函数
import psycopg2
import pandas as pd
import plotly.express as px
import dash
import dash_core_components as dcc
import dash_html_components as html
v_user=111
conn = psycopg2.connect(host="xxxx", port = 5432, database="xxxxx", user="xxxxx", password="xxxxx")
statment= f""" select month, count(id) as sales from public.orders where user_profile_id={v_user} group by 1"""
df_orders= pd.read_sql_query(statment ,con=conn)
df_orders
app = dash.Dash()
fig = px.bar(df_orders, x="month", y="sales")
app.layout = html.Div([
html.H1('Sales by Users'),
html.Div([dcc.Graph(id='graph_1', figure=fig)])
])
if __name__ == '__main__':
app.run_server(debug=True, use_reloader=False)
@app.callback(
Output(component_id="graph_1", component_property="figure"),
[Input(component_id="{add the component that hold the user id}", component_property='value')]
)
def update_dates(user_id):
conn = psycopg2.connect(host="xxxx", port = 5432, database="xxxxx",
user="xxxxx", password="xxxxx")
statment= f""" select month, count(id) as sales from public.orders where
user_profile_id={user_id} group by 1"""
df_orders= pd.read_sql_query(statment ,con=conn)
return px.bar(df_orders, x="month", y="sales")
您需要了解您的用户 ID 在哪里以及何时更改