I’m a beginner coder, so please pardon the basicness of the following questions…
I’ve been trying to figure out a good way to pull up a “dynamic index card” of info on a dash application.
For example, let’s say I have a set of companies stored in a dataframe.
import pandas as pd
import numpy as np
df = pd.DataFrame({ 'company' : ['Big', 'Blue', 'Waffle', 'Schmoo'],
'product' : ['apples', 'shoelaces', 'ice', 'anxiety'],
'employees' : [2, 4, -1, 70]
})
df= df.set_index('company')
df
What I would like to be able to do is select a company name from a dropdown menu, and generate a little report about that company. For example:
Company name: Blue
Number of employees: 4
Main product: shoelaces
However, I’m having trouble getting line breaks in the text.
Below is what I tried:
import dash
import dash_core_components as dcc
import dash_html_components as html
import pandas as pd
import numpy as np
df = pd.DataFrame({ 'company' : ['Big', 'Blue', 'Waffle', 'Schmoo'],
'product' : ['apples', 'shoelaces', 'ice', 'anxiety'],
'employees' : [2, 4, -1, 70]
})
df= df.set_index('company')
inv_options = df.index
app = dash.Dash()
app.layout = html.Div([
html.H2('Company Report'),
html.Div(
[
dcc.Dropdown(
id='Company', # 'id' is also the column name in df
options=[{
'label':i,
'value':i
} for i in inv_options],
value ='Blue'),
],
style={'width': '25%',
'display': 'inline-block'}),
dcc.Markdown(id ='report-text')
])
@app.callback(
dash.dependencies.Output('report-text', 'children'),
[dash.dependencies.Input('Company', 'value')])
def callback_text(Company):
investor_str = '''Company name: {0}
Number of employees: {1}
Main product: {2}'''.format(
Company,
df.loc[Company, 'employees'],
df.loc[Company, 'product']
)
return investor_str
if __name__ == '__main__':
app.run_server(debug=True)
The result is:
I know that Markdown requires two spaces after a line to start a new line, but for some reason the spaces in the string code after Company name: {0}
disappear whenever I save the file.
I also tried to use html.Div()
instead of dcc.Markdown
, but I couldn’t figure out how to create line breaks without creating a new callback for each line.