冲刺四个具有相同输入和不同输出的回调

时间:2019-07-24 07:33:23

标签: python-3.x plotly-dash

使用dcc.store将数据从下拉列表传递到所有其他不同的回调中,我将从下拉列表中选择的值存储在dcc.store中。

试图使用dcc.store存储从下拉列表中选择的数据,并将数据传递给所有其他回调。一个回调用于生成动态选项卡,另一个回调(render_graph)用于根据选项卡选择生成图形,另一个回调(render_table)用于生成另一个数据表,另一个回调(render__checklist_graph)用于基于复选框选择生成图形,但由于将错误加载为依赖项而得到错误。

import psycopg2 as pg
import pandas.io.sql as psql
import pandas as pd
import dash
from dash.dependencies import Input, Output
import dash_core_components as dcc
import dash_html_components as html
import dash_table as dt
import plotly.graph_objs as go


conn = pg.connect(database="db",user="user", password="password")

df = pd.read_sql('SELECT * from "table"', conn)

external_stylesheets = ['https://codepen.io/chriddyp/pen/bWLwgP.css']
app = dash.Dash(__name__, external_stylesheets=external_stylesheets)

app.layout = html.Div(children=[
    html.Div([dcc.Store(id='memory-output')]),
    dcc.Dropdown(id='my-dropdown', options=[#dropdown
        {'label': i, 'value': i} for i in df.number.unique()
    ], multi=False, placeholder='Select Number...'),
    html.Div(id='output-container'),#dynamic tabs
    html.Div([# dynamic graph1 based on tab selection
        dcc.Graph(id='graph', figure={'layout':{'xaxis':{'showgrid':False,'zeroline':False,'showticklabels':False},'yaxis':{'showgrid':False,'zeroline':False,'showticklabels':False}}})]),#plot
    html.Div([#table
        dt.DataTable(id='data-table')
    ]),
    html.Div([# dynamic checkbox
        html.H5('Multiple Graphs'),
        dcc.Checklist(id='checklist', values=[])]),#checklist
    html.Div([#dynamic graph2 based on checkbox selection
        dcc.Graph(id='checklist-graph', figure={'layout':{'xaxis':{'showgrid':False,'zeroline':False,'showticklabels':False},'yaxis':{'showgrid':False,'zeroline':False,'showticklabels':False}}})])#plot
])

app.config['suppress_callback_exceptions'] = True

@app.callback([Output('output-container', 'children'),
              Output('checklist','options'),
              Output('memory-output', 'data')],
              [Input('my-dropdown', 'value')])
def update_output(value):
    number=str(value)
    df_number=df.loc[df['number'] == str(value)]
    measures_name=dfnumber.numbername.unique()
    #below options for checklist(checkbox)
    options=[{'label':i, 'value':i} for i in measures_name]
    values=[]
    return html.Div([# dynamic tabs
        html.H5('Measurements'),
        dcc.Tabs(id="tabs-example", value='tab-1-example', children=[
            dcc.Tab(label='{}'.format(name), value='{}'.format(name)) for name in measures_name
        ])
    ]), options, number

@app.callback([Output('graph', 'figure')],
              [Input('tabs-example', 'value'),
              Input('memory-output', 'data')])
def render_graph(value,data):
    number=str(data)
    df_number=df.loc[df['number'] == str(number)]
    df_measures=df_number.loc[df_number['numbername'] == str(value)]
    numb=len(df_measures)+3
    x=list(range(0,numb))
    y=list(df_measures['number'])
    text_pin=df_measures['number'].tolist()
    text_meas=df_measures['numbervalue'].tolist()
    trace1=go.Scatter(
        x=x,
        y=y,
        mode='lines+markers+text',
        text=text_pin,
        textposition='top center',
        showlegend=False
    )
    trace21=go.Scatter(
        x=x,
        y=y,
        mode='lines+markers+text',
        text=text_meas,
        textposition='bottom center',
        showlegend=False
    )#lines
    figure={
        'data': [trace1,trace21],
        'layout':{
            'xaxis':{
                'showgrid':False,
                'zeroline':False,
                'showticklabels':False,
                },
            'yaxis':{
                'showgrid':False,
                'zeroline':False,
                'showticklabels':False,
            },
        }
    }
    return figure

@app.callback(Output('data-table', 'data'),
              [Input('tabs-example', 'value'),
              Input('memory-output', 'data')])
def render_table(val,dat):
    number=str(dat)
    df_number=df.loc[df['number'] == str(number)]
    df_measures=df_number.loc[df_number['numbername'] == str(val)]
    df_table=df_measures[['number']]
    data=df_table.to_dict('records')
    return data

@app.callback(Output('data-table', 'columns'),
              [Input('tabs-example', 'value')])
def fetch_columns(va):
    columns={'number':'one','number2':'two'}
    return columns


@app.callback(
    Output('checklist-graph', 'figure'),
    [Input('checklist', 'values'),
     Input('memory-output', 'data')])

def redner_checklist_graph(v,d):
    number=str(d)
    df_number=df.loc[df['number'] == str(number)]
    figure={
        'data': [go.Scatter
                 (
                     x= df_number[df_number['numbername']==str(vi)]['number'],
                     y= df_number[df_number['numbername']==str(vi)]['numbervalue'],
                     mode='lines+markers',
                     marker={
                          'size': 15,
                          'opacity': 0.5,
                     }
                 )
                 for vi in v]
        }
    return figure

if __name__ == '__main__':
    app.run_server(debug=False)





Are there any alternatives to pass the stored data(dcc.store) to all those callbacks?

0 个答案:

没有答案