Reputation: 367
I have a code on Jupyter that works like this:
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
Now, I'd like to put this code online for my colleagues, but I never used Dash. The user should only:
Use an input box
Press confirm
Obtain the graphs
I need to save the input as a variable, but how to do this? I'm following this example:
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)
Is it possible save the value from the box as a python variable? Thanks
Upvotes: 1
Views: 4178
Reputation: 6616
Certainly. You can change the callback function like this:
@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)
Give it whichever name you like, and use it from there like any Python variable.
Edit:
Perhaps I should mention that the arg, input_value
is already a Python variable, which you can already use just like you would any other.
Upvotes: 2