我想根据破折号
Input
和其他一些“字符串”值返回一个表格。这个想法是基于相同的 Input
但不同的 string
值在多个地方重复使用相同的回调。现在,我的回调是这样的:
@app.callback(
[
Output('table', 'data'),
Output('table', 'columns')],
[Input('dropdown', 'value')]
)
def update_output(selected_type):
#(code continues)
## referred in app as:
app = dash.Dash(__name__)
app.layout = html.Div([
html.H1("Some dynamic table")
dcc.Dropdown(
id='dropdown',
options=['X','Y'],
value='X'
),
dash_table.DataTable(id='table') # arguments to update_output() based only on Input dropdown
])
我希望对其进行调整,以便保留
dropdown
输入,但我可以在函数的参数上指定一个字符串。像这样的东西:
@app.callback(
[
Output('table', 'data'),
Output('table', 'columns')],
[Input('dropdown', 'value')]
)
def update_output(selected_type, some_string='dog'):
#(code continues)
## referred in app as:
app = dash.Dash(__name__)
app.layout = html.Div([
html.H1("Some dynamic table")
dcc.Dropdown(
id='dropdown',
options=['X','Y'],
value='X'
),
'''This is table with dog:'''
dash_table.DataTable(id='table', 'dog') # arguments to update_output() based on Input dropdown, and I specify the string "dog" here myself
'''This is table with cat:'''
dash_table.DataTable(id='table', 'cat') # arguments to update_output() based on Input dropdown, and I specify the string "cat" here myself
])
也许我让它变得比需要的更复杂?最终目标是应根据用户
Input
和我的规范返回一个表。当然,一个简单的解决方法是为我自己的每个参数设置一个回调(例如 update_output_cat()
和 update_output_dog()
),但这并不是一个很好的解决方案
为此,您可以使用关键字“State”,例如设置默认值“dog”的文本输入。
这是您的代码的示例:
@app.callback(
[
Output('table', 'data'),
Output('table', 'columns')],
[Input('dropdown', 'value')].
State('table-name', 'value')
)
def update_output(selected_type, some_string):
#(code continues)
## referred in app as:
app = dash.Dash(__name__)
app.layout = html.Div([
html.H1("Some dynamic table")
dcc.Dropdown(
id='dropdown',
options=['X','Y'],
value='X'
),
dcc.Input(id="table-name", type="text", placeholder="", value="dog"),
'''This is table with dog:'''
dash_table.DataTable(id='table', 'dog') # arguments to update_output() based on Input dropdown, and I specify the string "dog" here myself
'''This is table with cat:'''
dash_table.DataTable(id='table', 'cat') # arguments to update_output() based on Input dropdown, and I specify the string "cat" here myself
])
查看有关 Input 的文档。