我在Jupyter上有一个代码,工作原理如下:
You input a keyword
The code makes a couple of API calls based on the keyword
The code merges and wrangles the optained databases
The code plots with Plotly
现在,我想为我的同事把这段代码放到网上,但我从来没有使用过达世币。用户只应:
Use an input box
Press confirm
Obtain the graphs
我需要将输入另存为变量,但如何做到这一点?我正在遵循此示例:
import dash
import dash_core_components as dcc
import dash_html_components as html
from dash.dependencies import Input, Output
external_stylesheets = ['https://codepen.io/chriddyp/pen/bWLwgP.css']
app = dash.Dash(__name__, external_stylesheets=external_stylesheets)
app.layout = html.Div([
dcc.Input(id='my-id', value='initial value', type='text'),
html.Div(id='my-div')
])
@app.callback(
Output(component_id='my-div', component_property='children'),
[Input(component_id='my-id', component_property='value')]
)
def update_output_div(input_value):
return 'You've entered "{}"'.format(input_value)
if __name__ == '__main__':
app.run_server(debug=True)
是否可以将框中的值保存为 python 变量?谢谢
当然。您可以像这样更改回调函数:
@app.callback(
Output(component_id='my-div', component_property='children'),
[Input(component_id='my-id', component_property='value')]
)
def update_output_div(input_value):
my_variable = input_value
return 'You've entered "{}"'.format(my_variable)
给它起你喜欢的任何名字,然后像任何Python变量一样使用它。
编辑:也许我应该提到 arg, input_value
已经是一个 Python 变量,你已经可以像使用任何其他变量一样使用它。