【发布时间】:2021-10-07 09:01:26
【问题描述】:
我使用 PostgreSQL 查询作为 Plotly Dash Dashboard 的数据源。 在查询开始时,我通过 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”只能看到它的数据。 怎么做? 或者也许还有另一种方法可以为每个用户过滤和显示不同的数据??
【问题讨论】:
标签: python postgresql plotly-dash plotly-python