Hi everyone!
I am currently learning dash and I’m having a great experience.
I am looking for a mentor who will guide me for a project I am working on since I am very new to programming. The deadline is approaching fast so I’d really appreciate if someone could help me out with it as I’ve been stuck on it for months.
I have a dropdown with names of students. I want that whenever I am able to choose a student from the dropdown, it should generate a table (which shows the number of books issued, category of books, and fine pending). The data is from an Excel sheet. But I really do not know how to go about it.
First is how to pull data from excel to a format that is ok to use in python y recomend for this to use pandas you need to install it by using pip install pandas
after that you can read the excel file whenever you want by using:
import pandas as pd
df = pd.read_excel (r’Path where the Excel file is stored\File name.xlsx’)
This is really usefull because now you have a dataframe which is ridiculously good at filtering data so you can easily filter by date,user,type of book and so.
Second what to use to display the data for this I recommend dash table which is a dash plugin you install by using pip install dash-table after this you can show nice tables pretty easy by following the examples in the official documentation https://dash.plot.ly/datatable i recommend you check out this first example directly pulld
third how to refresh the data for this wee need a callback that fires some changes anytime you change you dropdown. Here inside the callback you can load the excel sheet filter it and return the dash table to you html layout div where you wanna show the table as a result so basically inside here you can put the previous 2 steps a very resumed version of this would look like this.
Hey!
People like you are the reason why I love this community so much. Thank you for coming forward and help people like me to find solutions to their issue. I really appreciate your effort and time to explain me the concepts.
You have explained it wonderfully and ensured that I understand even the basic concept. Thank you for that! Really!
I’ll definitely try your code and let you know.
And if you do not mind me asking, one question I have is , in ‘df = filter_dataframe_with_criteria()’ , does the data frame be needed to be filtered already or can Python do that too? Say, if I want to filter it by number of books issued or by category (like we do in Excel Pivot tables), is it possible? And if yes, how?
Thanks for the regards. Of course you can pandas Dataframes are just a slightly enhance excel if you like to picture it that way.
if you have a column of categories you just need to filter it like
df_filtered = df[df[‘category’]==‘romatic’]
Same story with number of books but even better becuase you can filter by range like this
df_filtered = df[df[‘books_issued’]<=3]
df_filtered = df[df[‘books_issued’]==3]
df_filtered = df[df[‘books_issued’]<=3 & df[‘books_issued’]>1 ]
So if I have to display a table by selecting a student’s name from the dropdown (which will show all the books he/she has issued and their categories), I’d have to simply put a condition matching that student’s name. For example, df_filtered=df[df[‘student name’]==‘Steve’].
What if I have over a 100 students and there might be addition of new students in the database? What can we do in that case?
By the way, thank you once again, I am really learning from you.
first filter is correct that’s the way you filter by student.
with your second question it really depends on how you wanna approach the problem if all you have is a single excel file maybe you could consider split the info by by class for example in many sheets so you can have a less data in you dataframe if you sump data from a database maybe consider doing queries to the database by every student could be a solution. anyhow 100 students is not that much for a dataframe in pandas maybe that’s a future problem. in the meantime maybe you can kepp using the same file and just apply a filter before students filter to make the filtering faster for example filtering first by class and then by name so there is less data to filter you can also pre sort the data when you load your excel file and create an index base on you searching criteria for example if the students have a code_student you can set this column as an index and sort the dataframe by index then when you decide to filter this it would be faster.
if you want to do this I recommend you don’t load the excel in the callback instead you can load the dataframe into a hidden div in your html at the beggining of you program and that way you only have to load and sort the information once and do the filtering in the callback. you can find a good example on how to do that in this site: https://dash.plot.ly/sharing-data-between-callbacks
global_df = pd.read_csv('...')
app.layout = html.Div([
dcc.Graph(id='graph'),
html.Table(id='table'),
dcc.Dropdown(id='dropdown'),
# Hidden div inside the app that stores the intermediate value
html.Div(id='intermediate-value', style={'display': 'none'})
])
@app.callback(Output('intermediate-value', 'children'), [Input('dropdown', 'value')])
def clean_data(value):
# some expensive clean data step
cleaned_df = your_expensive_clean_or_compute_step(value)
# more generally, this line would be
# json.dumps(cleaned_df)
return cleaned_df.to_json(date_format='iso', orient='split')
@app.callback(Output('graph', 'figure'), [Input('intermediate-value', 'children')])
def update_graph(jsonified_cleaned_data):
# more generally, this line would be
# json.loads(jsonified_cleaned_data)
dff = pd.read_json(jsonified_cleaned_data, orient='split')
figure = create_figure(dff)
return figure
@app.callback(Output('table', 'children'), [Input('intermediate-value', 'children')])
def update_table(jsonified_cleaned_data):
dff = pd.read_json(jsonified_cleaned_data, orient='split')
table = create_table(dff)
return table