Hi,
I’m newbie to dash and I’m really excited to use it for my thesis. This is how my dash layout would look,
In Model definitions tab, I’ve several inputs as shown in figure. Those user inputs I want to get into a JSON. This JSON would be passed as an argument to API developed in python.
Here is my code to get the layout.
import dash
import dash_html_components as html
import dash_core_components as dcc
import json
from dash.dependencies import Input, Output, State
app = dash.Dash(__name__)
tabs_styles = {'height': '54px'}
tab_style = {
'borderBottom': '1px solid #d6d6d6',
'padding': '6px',
'fontWeight': 'bold',
'textAlign':'center'
}
tab_selected_style = {
'borderTop': '1px solid #d6d6d6',
'borderBottom': '1px solid #d6d6d6',
'backgroundColor': '#119DFF',
'color': 'white',
'padding': '6px'
}
app.layout = html.Div([
html.H1('Optimizer', style={'textAlign': 'center', 'color': '#FFA500'}),
dcc.Tabs(id="tabs-styled-with-inline", value='tab-1', children=[
dcc.Tab(label='Model definition', value='tab_model', style=tab_style, selected_style=tab_selected_style),
dcc.Tab(label='Universe definition', value='tab_univ', style=tab_style, selected_style=tab_selected_style),
dcc.Tab(label='Back testing', value='tab_backT', style=tab_style, selected_style=tab_selected_style),
], style=tabs_styles),
html.Div(id='tabs-content-inline')
])
@app.callback(Output('tabs-content-inline', 'children'),
[Input('tabs-styled-with-inline', 'value')])
def render_content(tab):
if tab == 'tab_model':
tab_model_app = html.Div([
html.Div([
html.H4('Optimization goal'),
dcc.Dropdown(
id='opt_goal',
options=[
{'label': 'Return Max', 'value': 'return_max'},
{'label': 'MV Objectives', 'value': 'mv_obj'}],
style=dict(width='40%', verticalAlign='middle'))
]),
html.H4('Select constraints'),
html.Div([html.Label('Holding weights', style={'marginRight': 25}),
dcc.Input(id='holding_wt_lower', type="number", placeholder="Lower bound", min=0.05, max=0.3,
style={'width': 100, 'marginRight': 25}),
dcc.Input(id='holding_wt_upper', type="number", placeholder="Upper bound", min=0.05, max=0.3,
style={'width': 100, 'marginRight': 25, 'marginBottom': 10})
]),
html.Div([html.Label('Fully invested', style={'marginRight': 25}),
dcc.Input(id='fully_invested_lower', type="number", placeholder="Lower bound", min=0.05, max=0.3,
style={'width': 100, 'marginRight': 25}),
dcc.Input(id='fully_invested_upper', type="number", placeholder="Upper bound", min=0.05, max=0.3,
style={'width': 100, 'marginRight': 25, 'marginBottom': 10})
]),
html.Div([html.Button(id='reset-button', n_clicks=0, children='Reset', style={'fontWeight': 'bold',
'textAlign':'center', 'marginRight': 25}, title='Click to clear the inputs'),
html.Button(id='submit-button', n_clicks=0, children='Submit', style={'fontWeight': 'bold',
'textAlign':'center', 'marginRight': 25}, title='Click to optimize')
]),
html.Div(id='output-container')])
return tab_model_app
if __name__ == '__main__':
app.run_server(debug=True)
How can I get the user inputs in JSON?