Python Data Visualization Transcripts
Chapter: Dash
Lecture: Full app data filtering
Login or
purchase this course
to watch this video and the rest of the course contents.
0:00
Now that we have to find our layout. We need to create our call back. So let me show you what that callback looks like.
0:08
We're gonna have a lot more inputs and outputs that we've discussed in the past. So first we have our histogram as an output.
0:16
We have the scatter plot as a figure output as well. We're going to output our data table and that's a data type,
0:24
which something new and then the children is something new as well. But this is going to update the count that shows how many items are selected.
0:33
Our input values are going to be the year slider, the transmission list. We're going to have a scatter plot which is gonna send back
0:40
the selected data and then the button which will send us a number of clicks. So now let's build out our function that's going to do all this.
0:51
So now for our update figure we need the year range, the transmission list, the selected data and the number of clicks.
0:59
Now I'm going to use this global variable for number of clicks. This is something I had to look up online to figure out.
1:05
And the content that I found said that we may need to be a little bit careful about this. If you use in production in a multi user environment for our
1:14
purposes. It works well if you find yourself needing to do this in a production
1:18
environment, I would do a little more research to see if there's maybe some some updated code to do that. But once we take that value,
1:26
what I'm gonna do is take my data frame and filter it based on the year
1:30
between the two year ranges and then the transmission type being in that transmission list.
1:36
So now I have some filtered data we can use to create our histogram So now we have a histogram based on our filtered data frame here we
1:46
can create a scatter plot on the filter data as well. So now we have our scatter plot with the X.
1:53
And Y. And then we also have our hover data using the index as well as the make model and year.
2:00
And we need that index so that we can get the data into our table later And then I found some other code that I had to use to update the
2:10
layout. So when you for our scatter plot where we want to make it selectable the individual items to update our data.
2:18
I had to use this click mode equals event and ui revision equals true for that
2:24
to work properly. I also wanted to update our traces so that the selected marker
2:29
color is red. And we'll walk through exactly what that looks like later. And this was a little trick that I had to do some research to figure out
2:37
how to make this work correctly. And so I I pointed to the link from the plotly community too that would
2:45
update those traces to the correct color and also clean the selected data to none if we needed to and now what we're gonna do is set it up so that
2:57
if there was selected data I'll capture those points and then create an index list of
3:04
all of those points. And then filter our data frame based on that index so that I can show those points.
3:11
So this is a little bit of complicated code so take some time to understand exactly what I'm doing here to pull that data out.
3:19
But that's part of the reason why I wanted to include it because I think it's
3:22
something you want to do and it's it's a little tricky but it's basically just working with python data structures to get the right data structure out.
3:30
Then I want to show the number of points. If there is no data selected, I'm just going to show the top 10.
3:36
And then the final thing we need to do is make sure we return something.
3:40
So we're gonna return our histogram or scatter and then are filtered data frame as a dictionary. So in the records format.
3:50
So this is the way that the data needs to be returned so that our data table will work properly. And then I am also going to return the number of
4:00
points label that I calculated up here and then our final piece is to show the name, make sure that runs when we choose to run it.
4:13
So now I have the full file like I said, I think it's important that you take some time and make sure you understand all this
4:18
But I've gone through it real quickly and we'll show the actual application here in a minute and show how it relates to this code.