将回调添加到在其他回调中创建的命令中

问题描述 投票:0回答:1

我正在尝试创建一个短划线框,您可以在其中选择要为其选择范围的数据的选择框。由于复选框的数量是可变的,因此可以在复选框上进行回调。现在,我已经选择了三个虚拟复选框。因此,当选中所有复选框时,将有三个Ranges滑块,而选中一个复选框则有一个Ranges滑块。替换虚拟变量后,复选框的数量是动态的(可以是5、8等。取决于我正在读取的csv文件)。 Rangeslider必须减少csv文件中的行数。对于某个列(复选框),它必须介于rangeslider选择的值之间。我不知道如何在Rangesliders上创建回调以首先更新plot屁股,rangesliders的数量是可变的,并且它不知道要使用哪个ID。此外,似乎没有将ID添加到布局中。下面是我的代码:

import dash
import dash_core_components as dcc
import dash_html_components as html
from dash.dependencies import Input, Output
import numpy as np
import plotly.express as px
import plotly.graph_objects as go
import pandas as pd

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

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

df = pd.read_csv('CSV/merged_layers.csv')
# available_indicators = df.columns.unique()
available_indicators = ['PercBelowMSL', 'PopDens2015', 'RPC4.5']
slider_col = 'RPC4.5'
steps = (df[slider_col].max() - df[slider_col].min()) / 10



#   For the plotting see:
#   https://plotly.com/python/scatter-plots-on-maps/
#   https://community.plotly.com/t/python-dash-examples-with-scattergeo/7018
#   https://plotly.com/python/reference/#layout-title
fig = go.Figure(data=go.Scattergeo(
        lon = df['center_lon'],
        lat = df['center_lat'],
        text = 'Hier een text erin',
        mode = 'markers',
        marker_color = df['RPC4.5'],
        ))

fig.update_layout(
        geo_scope='world',
        width= 1000,
        height= 800
       )


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

df = pd.read_csv('CSV/merged_layers.csv')
# available_indicators = df.columns.unique()
available_indicators = ['PercBelowMSL', 'PopDens2015', 'RPC4.5']

app.layout = html.Div([

        html.Div([
            dcc.Checklist(
                id = 'col_list',
                options=[{'label': i, 'value': i} for i in available_indicators],
                value= available_indicators,
            ),
        ],
        style={'width': '48%', 'display': 'inline-block'}),

        html.Div(
                id= 'rangeslider',
        ),

    html.Div(
        dcc.Graph(
                id= 'Hotspot locations',
                figure= fig
                )
        )

])



@app.callback(dash.dependencies.Output('rangeslider', 'children'), #What to update, first the id                     where output is going to be update figure aspect
   [dash.dependencies.Input('col_list', 'value')]) #we need the value of the checlist with         id=col_list
def RangeSlider(col):
    rs_lst = []
    if col != None:
        for c in col:
            if df[c].isnull().any():
                min = np.nanmin(df[c].values)
                max = np.nanmax(df[c].values)

            else:
                min = df[c].min()
                max = df[c].max()

            steps = (df[c].max() - df[c].min()) / 20
            c = c.replace('.', '_')
            rs = dcc.RangeSlider(
                id= c,
                min= min,
                max= max,
                value= [min, max],
                step= steps/10,
                marks = {i: '{}'.format(round(i, 2)) for i in np.arange(min, max+2*steps, steps)}
            )
            rs_lst.append(rs)
    return rs_lst




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

我也尝试过:但是它使页面非常慢,并且只打印滑块的开头:

@app.callback(dash.dependencies.Output('Hotspot locations', 'figure'),
[dash.dependencies.Input('rangeslider', 'children')])
def func(children):
    print(children)
callback plotly plotly-dash
1个回答
0
投票

对于这种用例,我建议Pattern-Matching Callbacks

© www.soinside.com 2019 - 2024. All rights reserved.