# Callback pandas csv

**URL:** <https://community.plotly.com/t/callback-pandas-csv/33983>\
**Category:** Dash Python\
**Created:** [January 22, 2020, 2:38pm UTC](https://community.plotly.com/t/callback-pandas-csv/33983 "2020-01-22T14:38:54Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![jbetts97](https://avatars.discourse-cdn.com/v4/letter/j/74df32/32.png) [@jbetts97](https://community.plotly.com/u/jbetts97)\
**Post date:** [January 22, 2020, 2:38pm UTC](https://community.plotly.com/t/callback-pandas-csv/33983/1 "2020-01-22T14:38:54Z")

</div>

Hello,

I have multiple graphs and pie charts that use data from pandas pd.read\_csv.  
I also have a dropdown menu at the top of the web page (which updates variable chosenmonth)

**I have two csv files:**  
sales-jan.csv  
sales-feb.csv

I would like the user on the website to be able to choose which CSV data they see by using a dropdown menu.  
How can I do this?

Kindest Regards,  
Joseph

---

<div class="post-metadata">

**Author:** ![flyingcujo](https://avatars.discourse-cdn.com/v4/letter/f/e480ec/32.png) [@flyingcujo](https://community.plotly.com/u/flyingcujo)\
**Post date:** [January 22, 2020, 4:57pm UTC](https://community.plotly.com/t/callback-pandas-csv/33983/2 "2020-01-22T16:57:26Z")

</div>

The online documentation has plenty of examples on how to add/populate a dropdown so i won’t duplicate here. You can also use `dcc.Store` component(s) to hold the data from the 2 csv files. Then, within a callback that would create your graphs, you can pass the data via the state feature ( `State('<your dcc.State component id>', 'data')`. The callback could be triggered by the selection of the callback, and you can determine which selection was made by looking at the `value` property of the callback.

Depending on how you implement your solution, you can possibly benefit from `dash.callback_context` - It can be invoked at the start of a callback and provides a weath of info about the parameters of your callback.

If you have sample code that is not working, you can paste (using Markdown) and the forum is pretty good about providing feedback .

---

<div class="post-metadata">

**Author:** ![jbetts97](https://avatars.discourse-cdn.com/v4/letter/j/74df32/32.png) [@jbetts97](https://community.plotly.com/u/jbetts97)\
**Post date:** [January 23, 2020, 3:45pm UTC](https://community.plotly.com/t/callback-pandas-csv/33983/3 "2020-01-23T15:45:27Z")

</div>

Hello flyingcujo,

Thanks for the response!

I’ve made a change so I now have a dcc.dropdown, and I have a callback which takes the dcc.dropdown in addition to a slider to change the figure of a pie chart. It works!

The only issue I have now is, I would like to do the same and update all my graphs depending on this input (the pie chart takes the slider input, but the other graphs do not).

Here’s what I have so far:

> import dash  
> import dash\_core\_components as dcc  
> import dash\_html\_components as html  
> import dash\_table  
> import pandas as pd  
> import plotly.graph\_objs as go  
> from dash.dependencies import Input, Output
> 
> external\_stylesheets = [‘[https://codepen.io/chriddyp/pen/bWLwgP.css](https://codepen.io/chriddyp/pen/bWLwgP.css)’]
> 
> app = dash.Dash( **name** , external\_stylesheets=external\_stylesheets)
> 
> df = pd.read\_csv(‘pcl-sales-jan.csv’)  
> df2 = pd.read\_csv(‘pcl-sales-feb.csv’)
> 
> app.layout = html.Div([  
> # Div1 - Heading  
> html.Div([  
> html.H1(‘PCL Performance Dashboard’),  
> dcc.Dropdown(  
> id=‘month-selector’,  
> placeholder=‘Select a month to view’,  
> options=[  
> {‘label’: ‘Jan’, ‘value’: ‘Jan’},  
> {‘label’: ‘Feb’, ‘value’: ‘Feb’},  
> {‘label’: ‘March’, ‘value’: ‘March’},  
> ])]),
> 
> ```
> # Div2 - Output Display from Month Selector
> html.Div(id='output-selection'),
> 
> # Div3 - Table
> html.Div([
> html.P([html.Br()]),
> dash_table.DataTable(
> id='table',
> columns=[{"name": i, "id": i} for i in df.columns],
> data=df.to_dict('records'))
> ], className="row"),
> 
> # Div4 - Pie chart and slider
> html.Div([
> dcc.Graph(id='pcl-sales-pie',
> style={'width': '120%'}),
> dcc.Slider(
> id='date-slider',
> min=df['Date'].min(),
> max=df['Date'].max(),
> value=df['Date'].min(),
> marks={str(Date): str(Date) for Date in df['Date']},
> step=None
> )
> ], className= "five columns"),
> 
> # Div5 - History Graph
> html.Div([
> dcc.Graph(
> id='history-graph',
> figure={
> 'data': [
> {'x': df['Date'], 'y': df['Orders Raised'], 'type': 'scatter', 'name': 'Orders Raised'},
> {'x': df['Date'], 'y': df['Orders Shipped'], 'type': 'scatter', 'name': 'Orders Shipped'},
> ],
> 'layout': {
> 'title': 'Sales History for Selected Month'
> }
> }),
> ], className="six columns"),
> 
> # Div6 - Forward Order/Back Order
> html.Div([
> dcc.Graph(
> id='forward-back-orders',
> figure={
> 'data': [
> {'x': df['Date'], 'y': df['Forward Sheets'], 'type': 'scatter', 'name': 'Forward Orders'},
> {'x': df['Date'], 'y': df['Back Sheets'], 'type': 'scatter', 'name': 'Back Orders'},
> ],
> 'layout': {
> 'title': 'Sheets from Forward Orders vs Back Orders'
> }
> }),
> ], className="eleven columns"),
> ])
> 
> ```
> 
> def find\_date(dayofmonth):  
> for index, item in enumerate(df[‘Date’]):  
> if item == dayofmonth:  
> return index  
> return 0
> 
> @app.callback(  
> Output(‘pcl-sales-pie’, ‘figure’),  
> [Input(‘date-slider’, ‘value’),  
> Input(‘month-selector’, ‘value’)])
> 
> def update\_figure(date\_slider\_value, month\_selector\_value):  
> ndx = find\_date(date\_slider\_value)
> 
> ```
> # if None is selected
> if month_selector_value == 'None':
> df = pd.read_csv('pcl-sales-jan.csv')
> 
> # if Jan is selected
> if month_selector_value == 'Jan':
> df = pd.read_csv('pcl-sales-jan.csv')
>     
> # if Feb is selected
> if month_selector_value == 'Feb':
> df = pd.read_csv('pcl-sales-feb.csv')
> 
> shipped = df['Orders Shipped'][ndx]
> open = df['Orders Open'][ndx]
> info = [shipped, open]
> 
> return {
> "data": [go.Pie(labels=["Shipped", "Open"],
> values=info,
> textinfo='label')],
> "layout": go.Layout(title=f'Shipped Performance for Selected Day',
> margin={"l": 300, "r": 300, },
> legend={"x": 1, "y": 0.7})}
> 
> ```
> 
> @app.callback(  
> Output(‘output-selection’, ‘children’),  
> [Input(‘month-selector’, ‘value’)])
> 
> def update\_output(value):  
> chosenmonth = value
> 
> # print (chosenmonth)
> 
> ```
> return 'You have selected "{}"'.format(value)
> 
> ```
> 
> if **name** == ‘ **main** ’:  
> app.run\_server(debug=True,host=‘0.0.0.0’)

The two csv files are below:

> **[Easyupload.io - Upload Files and Share Them Easily](https://easyupload.io/msdfn2)**
>
> Uplaod files easily and share them with your friends and colleagues. File upload solution for everyone. Upload big files and get a link to share effortlesly.

> **[Easyupload.io - Upload Files and Share Them Easily](https://easyupload.io/1chv68)**
>
> Uplaod files easily and share them with your friends and colleagues. File upload solution for everyone. Upload big files and get a link to share effortlesly.

Kindest Regards,  
Joseph
