分配函数以在python破折号中删除淹没元素

时间:2018-11-06 08:22:09

标签: python plotly-dash

我创建了一个仪表板,以CSV格式显示数据。但是我不确定如何将单独的功能分配给相对的下拉选项。下面是代码:

import dash
import dash_core_components as dcc
import dash_html_components as html
import pandas as pd
from dash.dependencies import Input, Output

import plotly.figure_factory as ff
import plotly.graph_objs as go

df1 = pd.read_csv('ReturnsData - returns_summary.csv.csv')
df2 = pd.read_csv('ReturnsData - NonReturn_guideid_counts.csv.csv')
df3 = pd.read_csv('ReturnsData - AllReturns.csv.csv')
df4 = pd.read_csv('ReturnsData - OrderCreationdetails.csv.csv')
df5 = pd.read_csv('ReturnsData - Return_guideid_counts.csv.csv')


def generate_table(dataframe,max_rows=1000000):
    return html.Table(
        # Header
        [html.Tr([html.Th(col) for col in dataframe.columns])] +

        # Body
        [html.Tr([
            html.Td(dataframe.iloc[i][col]) for col in dataframe.columns
        ]) for i in range(min(len(dataframe), max_rows))]
    )


external_stylesheets = ['https://codepen.io/chriddyp/pen/bWLwgP.css']

app = dash.Dash(__name__, external_stylesheets=external_stylesheets)

app.layout = html.Div([
    dcc.Dropdown(
        id='my-dropdown',
        options=[
            {'label' : 'Returns Summary' , 'value' : 'df1'},
            {'label' : 'NonReturn guideid counts' , 'value' : 'df2'}
        ],
        # value='df1'
        # multi = True
    ),
    html.Div(id='output-container')
])


@app.callback(
    dash.dependencies.Output('output-container', 'children'),
    [dash.dependencies.Input('my-dropdown', 'value')])
def update_output(value):
    return generate_table(df1)



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

在这两个选项中,我只能触发该功能以显示df1。选择第二个下拉菜单时如何触发df2?

1 个答案:

答案 0 :(得分:0)

您已经在df1方法中对generate_table进行了硬编码。您需要使此方法的参数动态化,具体取决于通过回调的value方法传递了什么update_output(这是用户从下拉菜单中选择的内容)。按照official Dash-Plotly docks中的示例,您需要以这种方式修改代码:

@app.callback(
    dash.dependencies.Output('output-container', 'children'),
    [dash.dependencies.Input('my-dropdown', 'value')])
def update_output(value):
    return generate_table(value)

如果您需要为df1df2使用不同的功能,则可以添加以下逻辑:

def update_output(value):
    if value == df1:
        return generate_table(value)
    elif value == df2:
        return other_generate_table_function(value)