带下拉菜单的短划线回调

2022-04-19 00:00:00 python pandas plotly plotly-dash

问题描述

我正在尝试弄清楚如何为具有多个国家/地区的情节实现下拉菜单。我对DASH和PARTHY是个新手。这是我的代码:

import pandas as pd
import plotly.express as px
import dash
import dash_core_components as dcc
import dash_html_components as html

df = pd.read_csv('https://api.statbank.dk/v1/data/mpk100/CSV?valuePresentation=Value&timeOrder=Ascending&LAND=*&Tid=*', sep=';')
df = df[df['INDHOLD'] != '..']
df['rate'] = df['INDHOLD'].str.replace(',', '.').astype(float)
available_countries = df['LAND'].unique()
df.groupby('LAND')

app = dash.Dash()
app.layout = html.Div([
    dcc.Graph(
        id='mpk100'
    ),
dcc.Dropdown(
                  id = "dropdown_mpk100",
                  options=[{'label': i, 'value': i} for i in available_countries],
                  value=['DANMARK', 'USA'], #Initial values,
                  multi=True)
    

@app.callback(Output(component_id='fig-mpk100',component_property='figure'),
    [Input(component_id='dropdown_mpk100', component_property='value')])

 def update_df('value'):
return dep_var

    
app.run_server(debug=True, use_reloader=False)  # Turn off reloader if inside Jupyter

使用此代码时,可用国家/地区的下拉列表或图表都不会显示在破折号中。谁能解释一下如何处理这一问题,并可能给出解决方案?


解决方案

代码中缺少几个部分。具体地说,您没有生成任何数字。在这里,我基本上筛选出要绘制的所有国家/地区的df,然后使用plotly.express将所有这些国家/地区绘制为线条。如果您愿意,最终可以使用plotly.graph_objs添加跟踪。

import pandas as pd
import plotly.express as px
import dash
import dash_core_components as dcc
import dash_html_components as html

df = pd.read_csv('https://api.statbank.dk/v1/data/mpk100/CSV?valuePresentation=Value&timeOrder=Ascending&LAND=*&Tid=*', sep=';')
df = df[df['INDHOLD'] != '..']
df['rate'] = df['INDHOLD'].str.replace(',', '.').astype(float)
available_countries = df['LAND'].unique()

app = dash.Dash()
app.layout = html.Div([
    dcc.Dropdown(
        id='demo-dropdown',
        options=[{'label': k, 'value': k} for k in available_countries],
        value=['Danmark', 'USA'],
        multi=True
    ),

    html.Hr(),
    dcc.Graph(id='display-selected-values'),

])

@app.callback(
    dash.dependencies.Output('display-selected-values', 'figure'),
    [dash.dependencies.Input('demo-dropdown', 'value')])
def update_output(value):
    ts = df[df["LAND"].isin(value)]
    fig = px.line(ts, x="TID", y="rate", color="LAND")
    return fig


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

相关文章