多输入仪表板输出

2024-05-01 21:17:02 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图有一个破折号组件正确输入变量,并给出适当的输出。在

当前多个输入将使功能无法工作。在

我在我的dcc下拉列表中输入multi=true-还没有成功。在

这是我用过的密码。在

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

import plotly.graph_objs as go

df = pd.read_excel('FreewayFDSData.xlsx', 'Volume', parse_dates=True, index_col="Time")
df = df.T

Detectors = list(df.columns)

mf = pd.read_excel('FreewayFDSData.xlsx', 'Coordinates')

mapbox_access_token = 'pk.eyJ1IjoibWFwYm94IiwiYSI6ImNpejY4NXVycTA2emYycXBndHRqcmZ3N3gifQ.rJcFIG214AriISLbB6B5aw'

#  Layouts

layout_map = dict(
    autosize=True,
    height=500,
    font=dict(color="#191A1A"),
    titlefont=dict(color="#191A1A", size='18'),
    margin=dict(
        l=35,
        r=35,
        b=35,
        t=45
    ),
    hovermode="closest",
    plot_bgcolor='#fffcfc',
    paper_bgcolor='#fffcfc',
    legend=dict(font=dict(size=10), orientation='h'),
    title='Freeway detectors',
    mapbox=dict(
        accesstoken=mapbox_access_token,
        style="light",
        center=dict(
            lon=145.061,
            lat=-37.865
        ),
        zoom=12,
    )
)


def generate_table(dataframe, max_rows=10):
    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))]

        #Styling

    )


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

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

app.layout = html.Div(children=[

    html.H1(children='Managed Motorway'),

    html.Div([

        html.Div([
            dcc.Dropdown(
                id='xaxis-column',
                options=[{'label': i, 'value': i} for i in Detectors],
                value='Volume per 15 seconds',
                style={"width" : '48%'}
            ),
            dcc.RadioItems(
                id='xaxis-type',
                options=[{'label': i, 'value': i} for i in ['Linear', 'Log']],
                value='Linear',
                labelStyle={'display': 'inline-block'}
            )
        ]),
        dcc.Graph(id='indicator-graphic'),
        dcc.Graph(
            id='graph',
            figure={
                'data': [{
                    'lat': mf.Y, 'lon': mf.X, 'type': 'scattermapbox'
                }],
                'layout': layout_map
            }
        )
    ], style={'display': 'block'}),

    html.Div([
    html.H4(children='Example of Freeway FDS Data'),
        html.Div([
        generate_table(df)
    ], style={'overflowX': 'scroll','overflowY': 'scroll', 'width':'48%','height':'300px'})
])
])


@app.callback(
    Output('indicator-graphic', 'figure'),
    [Input('xaxis-column', 'value'),
     Input('xaxis-type', 'value')])

def update_graph(xaxis_column_name, xaxis_type):
    # xaxis column name will assign the x axis data being sought
    return {
        'data': [go.Scatter(
            x=df.index,
            y=df[xaxis_column_name])]
    }


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

这是一个输入数据的例子。在

^{pr2}$

结果是一个改变了基线的错误,而不是一个变量的结果。在

任何关于解决这个问题的帮助都是非常感谢的(如果你能让我的代码不那么混乱,我会很感激的。在

干杯!在

调试完成后完成代码

def update_graph(xaxis_column_name, xaxis_type):
    graph = []

    if xaxis_column_name != None :
        for i in range(0, len(xaxis_column_name)):
            graph_obj = go.Scatter(
                x=df.index,
                y=df[xaxis_column_name[i]])

            graph.append(graph_obj)
        return {
            'data': graph
        }
    return

Tags: nameinimportdataframedfforvaluehtml
1条回答
网友
1楼 · 发布于 2024-05-01 21:17:02

我不能完全运行你的代码来调试它,我在快速浏览后发现了这一点。在

xaxis-columnDropdown组件更改为执行多选时,它将返回list,而不是value,因此对xaxis-column的回调将是错误的

把回调改成这样应该行得通

@app.callback(
    Output('indicator-graphic', 'figure'),
    [Input('xaxis-column', 'value'),
     Input('xaxis-type', 'value')])

def update_graph(xaxis_column_name, xaxis_type):
    graph = []
    for i in range(0, len(xaxis_column_name)):
        graph_obj = go.Scatter(
            x=df.index,
            y=df[xaxis_column_name[i]])

        graph.append(graph_obj)
    return {
        'data': graph
    }

相关问题 更多 >