Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x plotly dash中两个下拉列表的互连_Python 3.x_Flask_Plotly Dash - Fatal编程技术网

Python 3.x plotly dash中两个下拉列表的互连

Python 3.x plotly dash中两个下拉列表的互连,python-3.x,flask,plotly-dash,Python 3.x,Flask,Plotly Dash,我一直在尝试将两个下拉列表相互连接,即,如果我从一个下拉列表中选择一个值,则路径和另一个下拉列表的内容应相应更改。 e、 g我有一个日期文件夹(06-06-2020、07-06-2020和08-06-2020),因此如果我选择的日期为07-06-2020,则其下方的下拉列表应显示日期文件夹07-06-2020内的值 import dash import dash_core_components as dcc import dash_html_components as html from das

我一直在尝试将两个下拉列表相互连接,即,如果我从一个下拉列表中选择一个值,则路径和另一个下拉列表的内容应相应更改。 e、 g我有一个日期文件夹(06-06-2020、07-06-2020和08-06-2020),因此如果我选择的日期为07-06-2020,则其下方的下拉列表应显示日期文件夹07-06-2020内的值

import dash
import dash_core_components as dcc
import dash_html_components as html
from dash.dependencies import Input, Output
from dash.exceptions import PreventUpdate
import plotly.graph_objects as go
import os
import pandas as pd
os.chdir(r"C:\Users\ankit\Desktop\dash_assign\SessionCode")
dir_route = os.getcwd()
app = dash.Dash(__name__)
g_t = ["OHLC Bars", "Candlesticks"]
# external_stylesheets = ['https://codepen.io/chriddyp/pen/bWLwgP.css']
# csv_list = [files for files in os.listdir(dir_route)]
csv_list = []
date_list = []
path_list = []
for dates in os.listdir(dir_route):
    date = dates
    date_list.append(date)

app.layout = html.Div(
    children=[
        html.H1("Time Series Graph"),
        dcc.Dropdown(id="select_date",
                    options=[{"label": dates, "value": str(dates)} for dates in date_list],
                     value=20180102,
                     style={
                         "margin": "10px"
                     }
        ),
        dcc.Dropdown(id="sym",
                     placeholder="Select Symbol",
                     style={
                         "margin": "10px"
                     }),
        # html.Button("PLOT", id='plot'),
        dcc.Graph(id='graph')
    ]
)


@app.callback(Output('sym', component_property="options"),
              [Input('select_date', 'value')])
def update_dates(dat):
    lst = os.listdir(os.path.join(dir_route, dat))
    for files in lst:
        if files.endswith(".csv"):
            files.split(" ")
            new_file = files[0]

    return new_file

@app.callback(Output('graph', 'figure'),
              [Input('select_date', 'value'),
               Input("sym", "options"),
               ])
def update_graph(date, symbols):
    path = os.path.join(dir_route, date, symbols)

    df = pd.read_csv(os.path.join(path+".csv"), index_col=False)
    fig = go.Figure(go.Ohlc(x =df["TimeStamp"],
                                 open=df['Open'],
                                 high=df['High'],
                                 low=df['Low'],
                                 close=df['Close'],
                                increasing_line_color='#00ff00',
                                decreasing_line_color='#ff0000',
                                name="Price",
                                ))
    return fig
if __name__ == "__main__":
    # fig = update_candle("A.csv")
    # fig.show()
    app.run_server(debug=True, port=5001)
这是我的根目录,里面有各种csv,一旦我选择了日期,它们就会出现在下拉列表中

您可以使用回调更新第二个下拉列表。下面是从表中创建级联下拉列表的示例

@app.callback(
    Output(component_id='id_first_dropdown', component_property='options'),
    [Input(component_id='id_second_dropdown', component_property='value')]
)
def update_dp(filter_value):
    sql = "Select distinct second_dropdown_options from table where first_dropdown_value='" + filter_value + "'"
    df = pd.read_sql_query(sql, conn)
    df = df['second_dropdown_options']

    return [{'label': i, 'value': i} for i in df.unique()]

您可以使用回调来更新第二个下拉列表。下面是从表中创建级联下拉列表的示例

@app.callback(
    Output(component_id='id_first_dropdown', component_property='options'),
    [Input(component_id='id_second_dropdown', component_property='value')]
)
def update_dp(filter_value):
    sql = "Select distinct second_dropdown_options from table where first_dropdown_value='" + filter_value + "'"
    df = pd.read_sql_query(sql, conn)
    df = df['second_dropdown_options']

    return [{'label': i, 'value': i} for i in df.unique()]