image: thumbnail.jpg
Real-World Python Dashboard Project w/ Quarto! (Airbnb Review Analysis)
- Github Repo: https://github.com/KeithGalli/quarto-projects
- Check out the recent Quarto Crash Course Video! https://youtu.be/_VKxTPWDhA4?si=sf7cbvSJGj6gnlCl
- Check out Mine’s Quarto Dashboards with R Code Videos! https://youtu.be/HW7QbqI4fH0?si=oyJhLaJ0n3Vg4v7Y
- Learn more about Shiny for Dashboards! https://youtu.be/I2W7i7QyJPI?si=Osqk5odwSOZ0ub01
- Bootstrap Icons: https://icons.getbootstrap.com/
Video Timeline!
0:00 - Video Overview
0:41 - About our Data & Get Started with the Code
1:38 - Basic Quarto Dashboard Layout Information
5:08 - Adding Value Boxes to our Dashboard
10:56 - Prepping our Airbnb Data for our Dashboard
17:00 - Adding Matplotlib Data Visualizations to our Dashboard (line graph, bar chart)
26:26 - Add a Map of Airbnb Listings using Latitude & Longitude (Folium Library)
33:18 - Adding Additional Dashboard Pages with Interactive Tables (itables library)
42:23 - When to use Quarto versus Shiny for Dashboards?
44:00 - Using Shiny within a Quarto Project
49:14 - Hosting your Quarto Documents with Posit Connect Cloud
#python #quarto #dashboards #pythoncontent
Transcript#
This transcript was generated automatically and may contain errors.
Hey what's up everyone and welcome back to another video. In this video we're gonna see how we can build dashboards using Python and Quarto so that you can put together dashboards like the one you see on the screen with different types of components, different types of graphs, even different pages that have tables and some interactivity in them. We'll start with the basics and kind of work our way up to this.
There's no prereqs to watch this video but it might be helpful to check out the recent Quarto crash course we posted which I'll link in the description and if you work more in the R space I might also recommend checking out Mine's recent tutorials on Quarto dashboards which are also linked in the description.
Getting started with the data
To get started there's a github repo that has some data in it so it specifically has some Airbnb data both on all-time listings ratings and some specific reviews that the Airbnbs were given. This data came from a site called insideairbnb.com and basically downloaded some data from the Boston area there. I augmented it very slightly but I feel like it's a good data source to use to kind of build a real world-esque dashboard with Quarto and Python.
All of that's linked in the description. In addition the finished dashboard file that we'll have complete after this video will be found there and you can also see what libraries are needed via this requirements.txt but we'll start from scratch so I'm going to just delete all this stuff and the first thing we'll want to do is create a QMD file so I'll call this dashboard.qmd and when you're building a dashboard in Quarto we always want to start with specifying our parameters and specifically we'll specify the format dashboard.
And if some of this is not seeming familiar again there's a Quarto crash course linked in the description recommend checking that out but you don't need to watch that to see this video and we can go ahead and just render a basic dashboard so maybe I give this a title to rental dashboard and I can just add some random headers here how about that so we'll see we have a very basic dashboard given this gives us the title over here in the top left.
Rows, columns, and layout basics
I can just name this something like overview and I can start filling in contents of our dashboard here that makes sense. So the first thing is probably worth knowing about with Quarto dashboards is the concept of adding rows and adding cells within the row so we can add two rows here and basically we can add different Python blocks so I'm going to add Python code like this this is kind of standard Quarto stuff and you know maybe I'll just print hello world here and I'll copy this and paste it a few times just so you can kind of see how things are laid out and maybe I paste it twice in this second one.
So we have our overview page and we have two rows so this is a first row with four hello worlds and a second row with two hello worlds and I run this we see we get a layout like this. We can do some other stuff to this so imagine I wanted the height of this to be only like 10% I wanted to be much tighter and I wanted the rest of this stuff to fill in much more space I can start specifying different details of my rows like this so now our hello worlds up the top are much smaller and there's other ways we can modify this even further.
Other things worthwhile knowing about right at the start here is you can add like titles to your cards by doing the pound pipe syntax and doing something like title if you do that you'll see different parameters that you have access to so you know maybe if you wanted output to be false and not to show up that code you could do things like that there's different options we have here. But if I just add title rental overview we'll see that it gives it a little label like that. I can go down here let's say add it to this one and rerun this and we see the card pops up like this so we can start titling the different charts that we have in our dashboard.
Value boxes
Using this obviously though a bunch of hello worlds isn't the most useful thing so I recommend knowing what you have access to. So we have access to like showing all sorts of different plots in Python so I could use plotly here I could use matplotlib I could use Altair etc a bunch of different graphing libraries all could be embedded here. I also have access to some Quarto specific dashboard elements so one that I specifically like are these cards called value boxes.
So I'm going to remove this height real quick and maybe I might just call this like high-level stats. This won't show up in my dashboard anywhere but it's probably good to give a descriptive name so you know what type of code elements you want to include in that row. And I guess another thing worth mentioning is in addition to running our code like this we can also add this parameter orientation and make that I think rows let's see if this works and it basically just styles things — oh wait let's try it one more time — columns basically just formats things instead of this defining a row this now defines a column so everything within it is you know a row within that column so it's just reversing things but I'll remove that for now it's good to know about though.
And then if you wanted to specify a height or I guess a width of your row you would just similarly do width equals 30% when you have that orientation equals or orientation columns specified so we'll see that these will shrink just like that so some good basic stuff to know about. But let's get into some more interesting things so I mentioned these value boxes so let's add some value boxes and actually load in our data and just start doing some meaningful stuff.
If you ever forget about what value boxes are or how to use them I recommend just looking up value boxes Quarto go to the docs and you'll find all sorts of useful stuff. Somewhere on this page I'm sure we'll find the value boxes and sure enough here we are. I'm going to just copy an example value box maybe I'll do one that's a little bit more sophisticated so I'll do something like this and I will paste in this cell. And we don't have anything called comments right now so I'll just give this a random number okay so comments per day 45 and it's taking this primary color there's some built-in options here we could do something like secondary.
To run that it's going to take a secondary color based on a theme that we can specify in our yaml at the top. So if I did dark theme or let me think I think there's a theme called like Cosmo we'll probably see a different secondary color pop up and we'll see a different style. So looking into themes is another cool option here I'll just use the built-in theme but the secondary color here does depend on the theme you use. You can also though pass in just like random hex colors or I think some English named colors work but if I pass in a random hex color you know we get a bubble that looks like this.
All good stuff to know about these icons are interesting these come from the bootstrap icons so if we look at bootstrap icons I'll link this in the description. Anything we see here and anything we search up so imagine I'm doing a dashboard on Airbnb listen so maybe I want a house. If we copy the name house then we'll see that icon pop up it's all built in nicely to Quarto.
If we copy the name house then we'll see that icon pop up it's all built in nicely to Quarto.
So I'm going to paste in house and we'll see this icon change cool and maybe I wanted to change the title — title will be like number of listings that we have in our Airbnb. Okay that looks good maybe we did want this height to just feel like I don't know 25% of our space. And I'm going to paste in a couple more value boxes so I'll do one more one more so now we have three value boxes.
And how about we have one value box that's the number of listings one value box is number of reviews and then one's are maybe our average review rate. And make sense to have different values for each of these so number of reviews what might be a good icon maybe some sort of like piece of paper or something. I don't know what would happen if I look up count maybe yeah person — I kind of like that — number of reviews use the person one icon is just called person lowercase person. And then maybe this last one is going to be average review rating so maybe it's like a star I think that makes sense.
Zero star star or star fill I might use star fill star and now if we rerun this we get something that looks like this. And maybe I don't actually like star fill because it's — I thought I would just be white but it is filling in based on the theme I think — so I'm going to just leave it open cool. And I might just leave these colors as the secondary colors of the theme that we're using.
Loading real data
All right now let's actually fill in the real values here for these parts of our dashboard so let's load in some code. I often do this either you know maybe even above where I start my pages to kind of know this is like the setup code. I'm trying to go ahead and do Python here at the top and if we look at where our files are we have files called listings CSV and reviews dot CSV.
Let's go ahead and look at these so listings dot CSV — one cool thing within VS Code is you have the ability to preview CSVs. And this might be very small and hard for people to see try to zoom in a bit but has all sorts of information on different Airbnb listings. Again this is you know pretty much real data it's coming from this inside Airbnb site and this is for the Boston area. So we have some Boston Airbnbs here all sorts of information about them and we'll say that this is like the you know all-time ratings and whatnot. So you see there's like a ratings column in here somewhere — in here there's different ratings I think we're getting there — like review score different types of ratings out of five kind of all-time we'll say they are.
And we also have this reviews dot CSV table if I zoom in on this we have specific you know comments that people have left. Open up this column a bunch we actually have textual information about people's stays along with a rating that they gave the place. So in like a real-world context you might have these two different kind of data sources and you might kind of aggregate them and you know tie them together for your dashboard. Right now we're dealing with static data we're not like actually pulling from an API but we can kind of write our code in such a way where we could easily replace it with API calls if we needed to.
So what I might do here is I might create like a helper file I might call this like data helpers dot py and I might create some functions here. So I might import pandas as PD I might do like a def load listings and that would just return PD dot CSV data slash listings. We don't actually have a data folder maybe I should have put it that way — listings not CSV. I might have a function called load reviews that just loads the reviews.
This way the reason that I'm creating these functions is imagine that you did want to have some sort of sophisticated logic here. You wanted to be able to specify like you know a start date or something for the reviews an end date or maybe you wanted some API logic in here — you could put that all in the dashboard QMD file but it sometimes makes sense to you know keep things clean by isolating it and putting it in its own functions in its own file. So I'm just kind of showing that we can do it but we're gonna keep ours very simple it's just literally loading our CSV files.
But I'll make some sort of comment like showing functions that you could augment to have load data in — something like that. And then we go to our dashboard dot QMD we can import our — we probably do the same import — from data helpers import load listings and load reviews. We can set our reviews and our listings like this and that should be good there for getting our data.
And then if we wanted to actually populate our data I mean I guess one thing we could do is you can run cells here and actually see if things are loading properly. If I look up listings in this interactive table we see that we do get stuff. I could run commands like listings dot columns and just get the columns in that. I could do —
the same thing with like reviews and you know I try to make my text very big on the screen so you can all see but sometimes it as a result you don't see much meaningful stuff but we can access both that interactively by running cells here in our VS code window but we can go ahead and actually populate these with real values if I do length listings we'll get the number of listings in our data if I do length reviews we'll get the number of reviews and maybe this like load reviews was over a six month window you know it's just always whenever we run this Quarto report it's getting the last six months you could augment the function to do that and then average review rating this would be a little bit more complex we actually need to kind of know what columns and stuff we have in our reviews so let's just do that real quick reviews dot columns we have date month year let's look at it the head real quick month is a number that could very easily help us I guess we don't we just really just need to take an average over the overall for all the reviews we're not doing anything fancy right now so for this last value we could pass in some code here we already have it as a Python cell so let's think how we would do this yeah make it an f-string reviews overall was the column and I think we can just take the mean and I do like this syntax with f-strings showing show two decibel two decimal places as a float close this off and let's rerun our dashboard cool and now we actually have some real data here in our dashboard on you know some info on our listings and reviews now let's maybe add some useful graphs to our data so what we might want to know about is how have our listings been trending over time so maybe we add a row our next row is like plots and let's think what we might want to do here we might want to aggregate by month so we have our reviews and we have that month column we just saw so if we wanted to plot the data the average review by like month or something like that let's think how we could do that we could probably do a matplotlib plot pretty easily we could do maybe we import you know I kind of like loading in all my libraries at the top you could either do that at the top or you know do it in the cell that you need the code I think we just need these libraries for now if we plot we'll start out by doing a plot that is ratings by month that's going to be equal to reviews dot group by date I want to group by month specifically I guess the other way that they're doing it would work and then I want to take the mean but I only want the overall column and then I want to probably reset my index let's print ratings by month dot head reviews is not defined okay kind of run this cell first this looks pretty good is that all of the months that we have is there any more months I was just the head so I guess if I printed the whole thing we should see that six month cool you see this it looks good so now we just need to plot this and we could plot it by doing the following I could do PLT dot plot ratings by month month ratings by month overall this should do it rerun it look at that and you know you're going to want to customize it and style it a bit more so maybe you add a title like average rating past six months so we have a little title now for our our graph but I do wrong I think it's always a good idea to like add a thicker line I think just when you're dealing with this everything should probably be a bit bigger so and maybe we don't want instead of using numbers here we want to actually plot the month names I use github copilot in my vs code and I often will do something like make my matplotlib plot line thicker make the axis labels larger that's a good start see if it generates me good code I like this overall let's try this
Styling the matplotlib charts
what am I maybe even a little thicker try for here yeah it's a little bit easier to read I want the actual tick marks bigger so make plot tick marks bigger oh look at that let's see how this does okay it's showing all sorts of weird stuff now if you do a colon after your plot dot show it won't show these intermediate steps little quirk to know about did that do anything here let me make this like much bigger okay that's definitely making things bigger so you can kind of play around with this as you see fit that's a decent average rating chart and you know really as a someone that's running Airbnbs what I would want to know is like okay in the past six months are we trending upwards are we trending downwards that's gonna start helping me understand overall trends so maybe we're implementing a bunch of solutions to help improve our work you know based on this trend line I kind of have a sense of okay are the changes we're making actually working or not cool and then maybe we wanted to replace the month with the actual month name I think we can use the calendar library to do that I'm curious I could also replace numerical months one two three four five six with their correct month abbreviation Jan Feb March April June April shoot I forgot about May June let's see if this code works
yeah I mean this is gonna work just doing a quick replace yeah that did fix things well copilot is pretty crazy so I guess just if you're curious if you go in your VS code and go to extensions look up copilot that's what I'm using I think it's a paid product you can also check out something called codium as like a free way to do this in your VS code editor just helpful thing to know about all right so that's a ratings by month plot maybe we also wanted to add a plot that's just a histogram of the counts of each different type of rating so rating counts I'll call this that's going to be equal to reviews overall that value counts that sort index that looks right we can print that out real quick just to check run this and this looks good this looks right if we have a four point six four overall you'd expect most of them to be five stars and much less to be one and two and three stars so if we plot dot you can either do a bar chart or you could probably do a histogram via matplotlib cool and I think one thing that I would recommend is passing in a color that's similar to our secondary color so like what I might do open up a web browser real quick so if you touch this button you open up a web browser if we look at what the dashboard looks like in the web I could use I use this little inspector tool I can inspect this color have it and then I could you know make my line colors so I think somewhere in the plot I think I can pass in a color and now if we look at our line we see that's that same gray color similarly could pass in the color here get this to have that same curry I just like having uniformity in the colors that is in a dashboard that I'm working on you could also like as a better practice you might want to define your color at the top if there's a color you know you're going to be using and then just pass in that color in those different spots that takes in a color
I just like having uniformity in the colors that is in a dashboard that I'm working on you could also like as a better practice you might want to define your color at the top if there's a color you know you're going to be using and then just pass in that color in those different spots that takes in a color
Adding a folium map
cool all right let's maybe also add another row and we'll make an actual map of our listings the best way to do this I find is using this folium library so I'm going to import folium here up at the top and I usually forget how to do a folium plot without looking it up so I might just look up folium map Python or like let's say we have Latin longitudes because we do for our listing so folium lat and longitude Python example and I mean there's a bunch of different examples that you I'm sure you can find this looks like one too I'll just copy this little code snippet and paste it in here and our map center so let's look at our listings again we have columns called latitude and longitude here so we want to grab those so we have our listings they're going to be we want to grab the latitude and longitude the map center then is going to be listings dot latitude dot mean same thing for our longitude so we could either grab the column this way or you could use the bracket syntax I think that's a column is it a uppercase yep uppercase uppercase okay this is Boston and then we want to iterate basically over our listings and add markers like this so I could do is for index row and listings dot error rows look at that autocompletes it pretty well and let's see what our output looks like if we run this saying it doesn't know about a latitude that's weird I don't know if it's like auto capitalizing it it auto capitalizes it in the preview for whatever reason interesting
ok see what this does ok we're not seeing anything if I expand that we see this weirdness here and I know there's a solution to that but what we want to do to see our actual map is
just put my map at the bottom here with folium should display ok look at that we got our map hidden here at the end at the bottom we see all this weirdness you can fix the weirdness sometimes it shows intermediate outputs in your cells within your cards in a Quarto dashboard if you assign these intermediate steps to a variable will kind of fix that issue I also can add a height here which is I think a pixel value because right now if we scroll down we see that this is like this random height if I specify a height more explicitly it will help kind of center things so we actually see what we expect to on our screen if I pop this open pull this in now we have information again I maybe make these things a little bit bigger these values you know the labels and whatnot but shows are no different properties one cool thing that we can do is we can add custom markers here on a graph and we can even actually add information about each listing when we hover over these markers so I might paste something in this will make sense in a second I can add some custom HTML to each marker that we add so I might paste in something like this we add this basically custom tooltip that has our name the rating of the property and the price per night and then I can add a tooltip to our marker specified as tooltip and then I can also specify a icon I believe yeah icon folium icon color equals blue icon equals home let's see what this looks like
thing because we have a tooltip we don't need this pop-up do I screw something else up though this indentations off I think it should work now cool instead of having color blue there's only limited colors here in the folium icon we do color gray that looks pretty similar to our other colors I think there's even a light gray so you could pick one of those colors whatever you think is better but it kind of blends in with the rest of our details and you can see information about each of these listings you could zoom in you know look a little bit more about them all right cool let's add some different pages to our dashboard that has you know maybe some interactive tables so I'll add another page I'll call this like listing details and then I'll do another page called recent reviews I'm going to just kind of do some placeholders here okay let's look at that
Adding interactive tables
listing details recent reviews we want to add some tables here one library that I like a lot for showcasing tables in inside of Quarto is this I tables library so I'm going to do import I tables and then what we can do down here is we can just do something like Python I tables dot show and we can do just like our listings to start see what this looks like you see we got a table here it's got a lot of stuff in it so we probably only want to have certain columns instead of having everything but it is cool we could search then by like you know just ones in the south and how about just Beacon Hill so there's different ways you can search these interactive tables but maybe we wanted to just specify specific columns related to ratings so rating columns these are the basic columns that we want and now if I do listings how about rating let's see what this gives us
so now we have a table that looks like this which is pretty good obviously it's not super helpful though because we have these rating columns but we also would want like the name of the property or something so I might do like call this display columns and I might add you know my name here of the property maybe the neighborhood or something maybe I also add the price as one of my display columns rerun this and we see we get something that looks like this if I then open this up in its full window we get something that looks like this and one thing that's cool is if I had this if I was trying to share this with a team member to understand help them understand our rentals I would probably give them you know this table and tell them hey you know maybe look at like the reviews that I have the least amount of accuracy or you know the least cleanliness and we can kind of start targeting which homes to fix up based off of this you know etc so that's one table I found sometimes you can customize this and tweak this in a way that makes more sense like for example if you wanted to make sure it wrapped and fit into the space of your screen you can add some custom CSS to you know allow for this let me see how you might do that you might do something like in our import I tables we would maybe also want to do import I tables dot ops shins as something like this let me double check as opt and then you can do something like opt dot classes and specify some classes that should affect things I could define a class called wrap text and then I could basically define that class so I could create a new file called like style dot CSS the class name wrap text and I could even if you wanted to see like this work you know make this give it a certain color or something too and then in my dashboard QMD file and I do style or I think CSS it will do style dot CSS we run this and we see we get it wrapped basically in a different way I mean basically we can just affect it just formats it a bit differently I'm not going to go into huge details here but just kind of know that you could add this style dot CSS and you can style different elements on the screen as needed okay that's pretty good if we wanted recent reviews table we could do something similar we probably have to join our reviews with the listing so reviews joined might equal this basically and then we would want to display probably the name of the listing the date and the comments of our reviews and we could do then instead this reviews joined display columns and let's see what this table looks like cool I also might actually recommend adding an overall score and the reason for that is if you're doing an Airbnb listings review it's sometimes really helpful to sort by lowest rated comments and you can kind of use these reviews to help you identify specific things that you want to improve in your dashboard so I like adding the overall there again let's look at what this looks like in our web tab cool good search you know if I wanted to just only see things for comfy Beacon Hill studio number 13 you do it this way we see things dynamically update so you know if I only wanted the stylish downtown studio yeah I can start typing it in filter by that cool this all looks good I mean honestly that's a lot of the basics I think one thing that's worth showing is that we could also put like a plotly graph in here instead of using matplotlib so to do that a similar plotly graph would be something like this so we have let's say we wanted to show this based on the neighborhood of the listing some code here that does all of this and this will be in the final code that is shared in the github link oh it's using trying to review use reviews joined here to do this I'm going to define this up at the top
so we now see some information about different neighborhoods as part of our dashboard cool cool one thing that you might be wondering about as you start getting comfortable with building dashboards in Quarto versus let's say shiny is like when do you use one over the other I think that if you're you know sharing something kind of at static intervals you don't always need the data to be the most up-to-date it's totally fine to use Quarto one thing that I'd recommend is to do embed resources equals true run this we look at the files we have access to one of them is going to be this dashboard HTML file one cool thing about Quarto is that like this file this dashboard at HTML you could share it with you know a team member or something and they have access to this whole little dashboard just via that HTML file whereas if this was a shiny app they would you need to run a server or something to have this up and running so it's like kind of cool you could generate you know a static dashboard basically and you know do that on you know every month basis and then as long as someone can open up you know the HTML file in a web browser like this you can see all this information for the let's say last six months so yeah if it's a static intervals I think Quarto is good if you need it to always be living and you want additional interactivity so imagine you wanted additional interactivity where there was like a sidebar and you could toggle on or off different neighborhoods then it might make sense to go to shiny and we've posted tutorials on shiny building dashboards in shiny with Python I'll put a link for that in the description as well check out that series if you want to really understand some you know more interactive dashboards one cool thing that you can do though is you can actually specify server shiny and start combining Quarto with shiny so you can do stuff like add a sidebar and I'm going to just kind of preview this I'm not going to go into the details here maybe in a future video we'll go into the details but I could do like from shiny import reactive and UI and then I could do UI dot input checkbox group I could specify neighborhood this is a cool autocomplete and then I'll make
a static intervals I think Quarto is good if you need it to always be living and you want additional interactivity so imagine you wanted additional interactivity where there was like a sidebar and you could toggle on or off different neighborhoods then it might make sense to go to shiny
this a list specify a checkbox group like this and then basically have like toggle controls and these toggle controls actually updates the data in here so real quick like I guess preview of that is that maybe I also wanted all of these active just to be let's see
selected I also want this list I want all of them to be selected to start you could imagine we have a reactive calc def DF or like filtered reviews
that is like this so this basically takes our reviews joined but filters out the neighborhoods that are selected or only filters in the neighborhoods that are selected and then you could imagine down here where we render the table instead of having this code I might do something like at render table return filter reviews and I'll show you what this looks like
you go to that I think recent reviews table it's not showing up for whatever reason
I'm gonna real quick just try to make this put this in the same cell as my table down below
oops this should be is in input dot neighborhood like this
okay it was getting mad because I had this embed resources but as we can see here if I pop this open and we look at our dashboard now if I untoggle these we ultimately get no data so there's cool interactivity you can add by combining shiny with Quarto but I'm not going to go into huge details there right now I'm going to remove that shiny code quick
Publishing the dashboard
I'm going to put this back to basically what we had before and then a final step I would recommend is if you wanted to host this somewhere like so I shared that you can you know do the embed resources and then just share the dashboard HTML file with someone but if you wanted actual web URL that you could go to you could add this code to a github repo commit it added a Quarto dashboard and then push it and if you use a platform like posit connect cloud
and if the repo that you're working off of is public so I'm going to go to posit connect cloud I could publish a new Quarto project Quarto projects master and then dashboard dot QMD I can go ahead and publish this it looks like it's mad at me because I didn't commit that file called data helpers
Quarto find the repo as long as it's public and you specify the dashboard QMD file you can publish it and we see we get our dashboard I can make this full screen I can look at the different tables and look at recent reviews and I can even go ahead and I can share the URL with a teammate this way as well
I can even go ahead and I can share the URL with a teammate this way as well
all right that's all we're going to cover about Python dashboards in this video hopefully you found it useful if you enjoyed this video make sure to throw it a thumbs up subscribe to the channel if you haven't already and I guess actually as I'm concluding one thing that I should mention is that I ran into some issues with certain versions of this itables library so specifically for this tutorial I was using a itables 2.0 as my version so if you're running into any itables issues maybe check that out all right that's all we're going to cover in this video if you enjoyed this video throw it a thumbs up subscribe to the channel if you haven't already more Quarto and Python videos coming soon until next time everyone peace out