在 Dash (python) 中,滑块和单选项回调有问题
In Dash (python), I have problem with slider and radioitem callback
回调时我收到两个错误:
- 属性 "slidevalue" 与组件 ID 一起使用:
回调的输入项之一中的“ageslider”。
此 ID 分配给布局中的 dash_core_components.RangeSlider 组件,不支持此 属性。
- 类型错误:'NoneType'对象不可订阅
我的代码是:
dbc.Col([
html.Div(id='Title_Slider', children='''Milo na ena LOLA''',
style={'margin-bottom':'50px', 'text-align':'center'}),
html.Div(dcc.RangeSlider(id='**ageslider**',min=18,max=60,step=1,value=[18,60],
marks={str(Age): str(Age) for Age in data.Age.unique()}
), style={'width': '100%', 'padding': '0px 20px 20px 20px'}),
和
@app.callback(
Output('bar-1', 'figure'),
Input('dropdown1','value'),
Input('ageslider','slidevalue'),
Input('binat','bivalue'))
def update_line_chart(filter_metric, **vage** , bi_value):
v_min = **vage**[0]
v_max = **vage**[1]
#age_filter is a custom func that works
datac_ = age_filter(data, v_min, v_max)
a=datac_.groupby([filter_metric,'Attrition']).apply(lambda x:x[filter_metric].count()).reset_index(name='Counts')
fig = px.line(a, x=filter_metric, y="Counts", color='Attrition',markers=True,height=500, width=700)
fig.update_layout(plot_bgcolor=colors['background'],paper_bgcolor=colors['background'], font_color=colors['text'])
return fig
有谁知道为什么 RangeSlider 会遇到这个问题??
应该是value
而不是slidevalue
Input('ageslider','value'),
我无法测试第二个问题,但我认为 value
也应该可以解决它。
最小工作示例:
import dash
from dash import dcc, html
from dash.dependencies import Input, Output
import dash_bootstrap_components as dbc
import plotly.graph_objs as go
import pandas as pd
data = pd.DataFrame({
'Age': list(range(18, 61, 5))
})
app = dash.Dash(__name__)
app.layout = dbc.Col([
html.Div(id='Title_Slider', children='''Milo na ena LOLA'''),
dcc.RangeSlider(id='ageslider', min=18, max=60, step=1, value=[18,60],
marks={str(Age): str(Age) for Age in data.Age.unique()}
),
dcc.Dropdown(id='dropdown1',
options=[
{'label': 'New York City', 'value': 'NYC'},
{'label': 'Montreal', 'value': 'MTL'},
{'label': 'San Francisco', 'value': 'SF'}
],
value='NYC'
),
dcc.Graph(id='bar-1'),
html.Div(id='output-text'),
])
@app.callback(
Output('bar-1', 'figure'),
Output('output-text', 'children'),
Input('dropdown1','value'),
Input('ageslider','value'),
#Input('binat','value')
)
def update_line_chart(dropdown_value, slide_value): #, bi_value):
print('min_val:', slide_value[0])
print('max_val:', slide_value[1])
fig = go.Figure(data=[go.Bar(x=[1, 2], y=slide_value)])
text = f"{dropdown_value}: min: {slide_value[0]}, max: {slide_value[1]}"
return fig, text
if __name__ == '__main__':
app.run_server(debug=True)
回调时我收到两个错误:
- 属性 "slidevalue" 与组件 ID 一起使用: 回调的输入项之一中的“ageslider”。 此 ID 分配给布局中的 dash_core_components.RangeSlider 组件,不支持此 属性。
- 类型错误:'NoneType'对象不可订阅
我的代码是:
dbc.Col([
html.Div(id='Title_Slider', children='''Milo na ena LOLA''',
style={'margin-bottom':'50px', 'text-align':'center'}),
html.Div(dcc.RangeSlider(id='**ageslider**',min=18,max=60,step=1,value=[18,60],
marks={str(Age): str(Age) for Age in data.Age.unique()}
), style={'width': '100%', 'padding': '0px 20px 20px 20px'}),
和
@app.callback(
Output('bar-1', 'figure'),
Input('dropdown1','value'),
Input('ageslider','slidevalue'),
Input('binat','bivalue'))
def update_line_chart(filter_metric, **vage** , bi_value):
v_min = **vage**[0]
v_max = **vage**[1]
#age_filter is a custom func that works
datac_ = age_filter(data, v_min, v_max)
a=datac_.groupby([filter_metric,'Attrition']).apply(lambda x:x[filter_metric].count()).reset_index(name='Counts')
fig = px.line(a, x=filter_metric, y="Counts", color='Attrition',markers=True,height=500, width=700)
fig.update_layout(plot_bgcolor=colors['background'],paper_bgcolor=colors['background'], font_color=colors['text'])
return fig
有谁知道为什么 RangeSlider 会遇到这个问题??
应该是value
而不是slidevalue
Input('ageslider','value'),
我无法测试第二个问题,但我认为 value
也应该可以解决它。
最小工作示例:
import dash
from dash import dcc, html
from dash.dependencies import Input, Output
import dash_bootstrap_components as dbc
import plotly.graph_objs as go
import pandas as pd
data = pd.DataFrame({
'Age': list(range(18, 61, 5))
})
app = dash.Dash(__name__)
app.layout = dbc.Col([
html.Div(id='Title_Slider', children='''Milo na ena LOLA'''),
dcc.RangeSlider(id='ageslider', min=18, max=60, step=1, value=[18,60],
marks={str(Age): str(Age) for Age in data.Age.unique()}
),
dcc.Dropdown(id='dropdown1',
options=[
{'label': 'New York City', 'value': 'NYC'},
{'label': 'Montreal', 'value': 'MTL'},
{'label': 'San Francisco', 'value': 'SF'}
],
value='NYC'
),
dcc.Graph(id='bar-1'),
html.Div(id='output-text'),
])
@app.callback(
Output('bar-1', 'figure'),
Output('output-text', 'children'),
Input('dropdown1','value'),
Input('ageslider','value'),
#Input('binat','value')
)
def update_line_chart(dropdown_value, slide_value): #, bi_value):
print('min_val:', slide_value[0])
print('max_val:', slide_value[1])
fig = go.Figure(data=[go.Bar(x=[1, 2], y=slide_value)])
text = f"{dropdown_value}: min: {slide_value[0]}, max: {slide_value[1]}"
return fig, text
if __name__ == '__main__':
app.run_server(debug=True)