image: thumbnail.jpg
How to Perfect Your Python Dashboard with Advanced Styling! (HTML/CSS - Shiny for Python)
- Adding a logo and custom title
- Making use of custom HTML elements
- Using CSS to style Shiny components
- Customizing Altair charts for a polished look
- Advanced Plotly chart modifications
- Applying a consistent color theme and layout
By the end of this video, you'll have a styled dashboard, ready for a professional presentation.
Access the GitHub repo with all parts of this project: https://github.com/KeithGalli/shiny-python-projects
Shiny for Python Homepage: https://shiny.posit.co/py/
Check out the complete documentation here: https://shiny.posit.co/py/api/express/
Video by @KeithGalli
**Video Timeline:**
0:00 - Video Overview & Setup
1:50 - Modifying HTML and CSS in Shiny
7:44 - Adding a Logo Image
10:26 - Styling Labels and Containers (Aligning our Image w/ the Title — Custom Divs)
20:20 - Customizing Altair Charts (Gridlines, Font, Axis Labels, Etc.)
27:49 - Customizing Plotly Visualizations
37:16 - Customizing Seaborn & Folium Heatmaps
44:15 - Final Touches, Clean Up, Recap and Next Steps
If you enjoyed this series, give it a thumbs up and subscribe to the channel to stay updated!
All videos in the series:
Part 1 - How to Build, Deploy, & Share a Python Application in 20 minutes! (Using Shiny): https://www.youtube.com/watch?v=I2W7i7QyJPI&t=0s
Part 2 - How to make Interactive Python Dashboards! (Reactivity in Shiny): https://www.youtube.com/watch?v=SLkA-Z8HTAE&t=0s
Part 3 - How to make your Python Dashboard look Professional! (Layouts in Shiny): https://www.youtube.com/watch?v=jemk7DoN4qk&t=0s
Part 4 - How to combine Matplotlib, Plotly, Seaborn, & more in a single Python Dashboard! (Shiny for Python): https://youtu.be/xDgO5hB4-VU?si=kk20yhdpsBqkMYcC
Part 5 - How to Perfect Your Python Dashboard with Advanced Styling! (HTML/CSS - Shiny for Python): https://youtu.be/uYZUS-eFbqw
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 going to see how we can incorporate some advanced styling using HTML, CSS into our Shiny for Python apps.
So to get started you can go to this github link and all of the code and video resources for each of the previous parts of these series can be found in the github repo. But we're going to want to take the code from part 4, basically this app.py file and start from there.
As a reminder, basically in the last video we had an app that looked like this, pretty fully built out, but it just lacks some nice styling. So in this video we're going to see how we can turn this app into a more polished app that has a nice color scheme, etc. that looks like this.
Getting started with a new branch
So I think a good starting point, I like to create a new branch to work off so I'll call this video 5. Let's look again at what we're ultimately trying to do. Well we have a dashboard that looks like this and we're trying to convert it into one that looks like this.
So I think that a good approach here is like draw out what you want your design to look like and then ultimately like you modify your code to actually make that happen. So that's kind of what we're following. We have like this polished final product and we're trying to convert our existing code to mimic that. So I think it makes sense to start top down. So let's go ahead and start with this title and logo.
Modifying HTML and CSS in Shiny for Python
And I think a starting point, let's get some context on modifying HTML and CSS within our app before we actually modify this title. So within Shiny we have really useful abilities to actually directly modify the HTML and CSS of our pages.
So if you're not super familiar with the HTML and CSS on any web page you ever browse on you can right click and this is true in most browsers and click inspect. And then what that basically opens up is all of this HTML source code. So we can see that everything's kind of hidden within this body. We can see the different elements of our screen. So we can see that this H2 tag is our title and all of these different graphs are kind of found in these divs.
And then additionally if we pull this open we also see the styles of all of this. So I could hide some of these things and you would see the style change. But this is our HTML and this is basically our CSS. So we can actually modify that directly within our Shiny for Python code and start doing some pretty cool things.
To do this, I think a good starting point is to add a UI.tags.style attribute to your Python app. You could do this in the same file or you could create a separate file to do this. But basically anything that you put within this little element will modify the CSS in your app.
So what might we want to do? Well just for proof of concept, how about we change some of this visualization's background colors. I could inspect. I could see that we're in this div. I could see that we're in this sidebar. I can start looking at IDs of things. So we see we have this label class, control label. If I wanted to, let's say, change up the style of this label within HTML and CSS, I could do it via this.
So what was that called again? The class was control label and the ID was city label. So if I did class control label, this period right here specifies a class in CSS. And then let's say I just wanted to do as a proof of concept, like background color is blue, run this. We see the control label now has a blue background for all of these different elements.
I could have also targeted just this city one by doing specifically ID label right here, ID city label. So if I didn't want this one to change, but I only wanted this element to change, I could have done hashtag. So this now, the hash sign, forget the pound sign, specifies an ID in CSS. I could have done city label. I run this. We're going to see that this, oh, I spelled label wrong. If I spell this correct, we'll see that this is still blue, but that other label down here is no longer targeted by the class modification.
Maybe I wanted to change the font size to be like 50px. So we'll see this label now get much bigger. You could do all sorts of customizations. Obviously, I will not leave it like this because maybe you have a different take, but I don't think that this looks particularly good right now. But it's cool to see that we can modify, start modifying the style of random elements.
Adding custom classes and IDs to UI elements
What's even more useful than this though is that we can start giving elements on the screen. So for example, like this layout sidebar, I could give this full sidebar, a class label, something like custom sidebar, how about, and now I can modify this whole sidebar, the attributes of it by, it's a class. So it's period because whenever we're modifying classes, it's period, custom sidebar. And we'll see that the font size within that sidebar, I don't think there's any real font and the color should change.
For whatever reason, it's not changing. But if you ever find that a color is not changing when you expect it to change, another little helpful hint is you can do this hashtag or I keep saying hashtag, exclamation point important. And that will force the changes to take place. Again, making this blue does not help us much, but it's cool to know that we can do this. And that's by adding this class underscore. The reason we add the underscore is because in Python, class like this is a protected name. So basically to get around that, we have to do class underscore.
And if you wanted to modify the ID, likewise, you do ID underscore. And that would allow you to customize these specific elements. And you can do that with all UI elements within this. Like I could have passed in a class to our sidebar, which is just this portion of the sidebar, whereas the layout sidebar, I guess, defaults to this right section. You could have added a class there, et cetera. So all good to know. We're going to remove this though for now, but it's good to see that this is possible.
And if you wanted to modify the ID, likewise, you do ID underscore. And that would allow you to customize these specific elements. And you can do that with all UI elements within this.
Building the custom title and logo
So our goal right now with this first part is to modify this title. So in the final polished product, we had a logo in there and a white text with a blue background. The logo is right next to the title. So how can we make this happen? I'm going to start by going to posit.shiny. I want to go to the gallery. So shiny.posit.co.py. I'm actually going to go into, I think, components. And I'm looking to see how we can put in an image. I always find this components page super helpful. And as we see in this output section, there's a nice image. And lucky for us, this is already defaulting to the shiny logo.
So we see we have this code down here. This is using a checkbox. That's not really necessary for us. So we can just copy this and make modifications as need be. To grab this shiny image, I've left that in the GitHub repo. You can go to the main page and you can go into assets and you'll find, if you're on the master branch, you'll find shinylogo.png and click on that and save image as to download it locally. I recommend putting it within your project repo.
So if we look at our shiny Python projects, we have the sales folder. I would recommend putting it in the sales folder. That's where our app.py is. And maybe we'll make a directory called images. I'm going to do a copy, downloads, shiny logo to our images folder. Now if we look in images, we see we have shinylogo.png.
And now to incorporate that, we want to copy that little code snippet we were just looking at. Copy that. Go into our Visual Studio code. I don't really care where I put it to start with. So I'll just go ahead and put it at the top of my file. Copy this code too. I don't have that in here. I'll put it in here. And then we have ours not just in the directory. We have it in images and then shinylogo.png. We don't have this toggle. So we can just go ahead and return the image and see what that does. That looks pretty good to me. So I'm happy with that. But obviously it's not next to the title. So we're going to have to play around with this a bit.
Kind of moving into the next recommendation here is you can really utilize the HTML and CSS to your advantage here. What we could do is we could actually define our own divs. And by using divs strategically, it will make it a lot easier for us to strategically put this logo right next to our title. So I'm going to go ahead and do ui.div. And we're going to want to give this div a name. So I'll call this header container.
And it will be the exact same. You can use ui.div or you could also do ui.tags.div. I believe these are exactly the same. In the documentation, you'll probably see ui.tags.div, but the little shorthand is ui.div. And we can put the logo within this div. It's going to be the exact same right now. But we can start then getting even more creative. Within this header container, we can put another div. We'll call this. And maybe the proper technique here would be giving these IDs. I'm not an HTML, CSS expert. So as long as you can customize this as you need, I am not too picky whether you use classes or IDs. But I'll call this logo container. We'll put the logo in here. And then finally, we can do with ui.div, call this title container, how about. And then additionally, in addition to like ui.div, we could also do ui.h2 or tags.h2 and just pass in a title directly here. So I'll do ui.h2 and then make this sales dashboard video five of five.
This is starting to look better. I think the big issue we see here is now we have two copies of our title. We open this up. You'll see that this also says sales dashboard right here. I kind of want to keep these page options set here, but instead of having this set as title, I'll make this now window title. Whatever this is won't affect this title down here, but it will affect what this tab says right here. So you can keep it as sales dashboard video five of five, or you could just keep it like write it as final sales dashboard, whatever you think makes sense here, but around this now we'll see this title disappear.
Using an LLM to generate CSS for the header
But we do see that we have some serious issues with how the logos lined up. So maybe you're an HTML CSS expert. I personally am definitely not. So I love to tap into kind of the latest and greatest tools to help me out with this type of thing. So I might just go ahead and like copy this code snippet and then go to your LLM, your favorite LLM of choice, like chat GPT, for example. And I might write something like, can you make a white H2 title for me with a blue background and have it aligned side by side with my logo or, and I'm going to change this to, can you provide the CSS to make a white H2 title for me with a blue background and have it aligned side by side with my logo image for reference. Here is some shiny for Python code that I'm using to help with this. Paste that in, whether you're using chat GPT 4.0 or 4.0 or 3.5.
So it gave me some CSS to use. I'm going to just copy this, see what happens out of the box and make adjustments as needed. No longer need this custom sidebar that was just kind of to show things. Run this. Let's see what happens. Getting a little bit better. I don't know why this is so far above this.
Doing some inspection on this. You can sometimes play around with things right in here. I see that this logo container is taking up way more height than the title container. So I think if we make this header container a set size and adjust the logo container and the title container to fill it, that will probably help us out a lot. So I'm going to go ahead and make the header container, let's say like 60 pixels.
So header container we'll say has a height of 60 PX. See what happens now.
And then I'm going to go ahead and say that logo container has a height of 100%. So because header container is above logo container, this should also make the logo container have a height of 60 pixels.
And that's looking better. Definitely looking better. I think though this shiny logo is a bit too big. I don't know why exactly that's happening.
Actually, I see that the width is specified as 100 pixels here. I think we should probably just get rid of that and let it be whatever it's going to be. I guess now it's going to be absolutely massive.
Trying to think here. Can I do 100% and then say that this is important? Or maybe we need to specify specifically the image tag so we can do this two-step sequence to specify specifically the image tag. I could very easily specify that I want the image to be like 40 pixels. This is definitely going to work. I don't know if it will work.
It looks a bit better. What if I made this now 100%? That doesn't work properly. I'm going to say that this is 40 pixels.
This is, you know, for me, this is kind of a bit of how I operate with HTML and CSS. It's a little bit of playing around with things. You could use ChatGPT more or Copilot more as needed. Then what I will do here is I'm going to add a little bit of padding. So if the logo container is 60 pixels and this is 40 pixels, to get it perfectly centered here, if I add a padding, which will pad everything around that div, if I add a padding of 10 pixels, we should get a perfectly kind of centered logo.
Where did the logo go? Maybe it's getting confused because I didn't have a colon here. There we go. Look at that. That's pretty good. And I don't know if I want this margin right as big as that. If I make this 10, will it move it closer a bit? I think it did.
I think that's the, I know I'm pointing at the screen, but you obviously can't see that. I think that's this distance right here. That looks pretty good. Is it centered?
So I've underlined all of this, so I want the, I might like copy all of this and I might say, I've modified the style to this, but I still need the header container to be centered on the screen. Please add this.
I think this justify content center should have our fix. Yeah, let's go.
Styling the title and background color
We're almost there with the title. I really don't like this blue color. So going into our final app, I do like this blue that we ended up using in the final app. So I think if I inspect this, can I easily see the color here? Not sure if I can.
What I typically do when I'm trying to figure out specific colors on the screen is I'll usually download like a Chrome extension. I think any browser that you use will probably have some sort of suitable extension. This is called ColorZilla, I think. Click on this. Now I have a color and I can just immediately take this. And instead of saying that the background color is blue, I'll paste in this color and we get that nice color.
It's not filling the full screen though. So maybe what would be smarter than specifically filling this for the H2 tag is I could just go ahead and have this fill for the entire body element of our HTML. And look at that. We got a much nicer looking title in my opinion. We could play around with that a bit more, play around with like the margins and the padding. I am pretty happy with this.
I might just remove this border radius. Oh, that's just making, that won't actually affect anything here now that we have the background color the same. I just don't love this padding. I'm going to make this a bit small there. Can't tell if that's doing much. Yeah, I mean, like this is exactly what I'm saying. You can play around with this a bit more to get it exactly how you want, but I'm content with that. And that's some basics of HTML CSS styling.
Customizing the Altair chart
All right, next, let's start customizing our different charts here. And really we play around with the colors. We play around with, this doesn't have any grid lines, whereas before this had some grid lines. Also the axis has changed, like these labels is a bit different, whereas these are nicely capitalized. So I think again, starting top to bottom, let's play around with some of these things.
All right, so going into our Altair graph, I think the first thing we want to do is remove chart lines. We want to change up the titles, et cetera. Again, work smarter, not harder. You could look at the documentation, but I might just honestly copy like my code.
Okay, next, I want to modify an Altair graph. I want to remove grid lines, change my X axis and Y axis labels to be capitalized, maybe it'd be more clear, but to from month and quantity ordered to month and quantity ordered respectively. What else do we want to do? I want to make the font color blue and let's specify a font we can use for all of our charts and of type Arial. And is there any other big changes we wanted to make? I think that's the main things that I see.
I guess we maybe remove tick marks, remove tick marks, leave the numbers, just the actual mark. Here is the sample code I have. This is kind of how I craft a lot of these prompts. Copy this and let's see what happens, enter.
This looks pretty good, so we can use this to Alt X and Alt Y to do the changes that we want. One thing I don't love, I'd rather just have this be like a generic one size fits all thing, so I might say, I might just honestly pull that out myself as a variable.
So let's go ahead, copy this, go in here, paste in the updated code, make sure it's at the right level of depth, I think it was right here, okay, run this. We see that it did work. I don't love, why did the labels, the fonts not change colors?
All right, adjusting the colors, oh, title color, let's see. Okay, and I really don't like that this is all just defined here, so I'm going to pull this out as a variable, call this Font Props, and I'll call this Font Props, so I don't have to change it in two places when I modify this. Run this again, and cool, that looks much better.
I could do the same thing with the title, I'm sure. So where's the title defined? Title is here. See if this works. I don't know if it will. Nope. I think I could define this as, though, alt.title, and then pass in the access. Did that work? Title param says no.
See, I can't figure things out, but I can keep asking ChatGPT to help me. And one thing I don't love, I don't love this blue color, I would rather a darker color, so again, I might, like, look at this, I might do the ColorZilla, I might pick, like, this color, got it copied, and then I can paste in this color into here, into here. Now we have a better colored, you could play around with that more, maybe make it darker, but I like this color better than previously.
Now the only question is how do we change that title color? How can I change my title, sales over time, dash, dash, to use the same color as my axis labels? Good old ChatGPT. Yeah, configure title, looks like you'd do it here. Add that to our code, and it's just, yeah, a little bit of make some changes and test it and then figure out what you need to change and make some more changes. I will copy the same color. You could even make this color a variable if you wanted to so you're not passing around this random hex code, but we'll see that this will hopefully do what we want. Look at that. Looks good.
And how does that compare to our final? I think the only change I see is that these labels are horizontal instead of vertical. Again, I can just say make x-axis labels are like tick marks, horizontal instead of vertical item labels.
Looks like I can do a label angle in this one. Now, I think that if we made them both zero, it's totally fine because this one is already zero. You can decide whether this makes sense or not. It looks terrible if it's smooshed like this, but it looks good when it's spread out. So yeah, you be the judge if this makes sense. Double check that everything works. Looks like the Altair's good, so let's keep moving on.
Customizing the Plotly charts
Let's make some modifications now to these lower charts. And so how do these compare? If we look at our final one, this is a nice blue color spectrum. It also has no grid lines. It has no background color. Let's use this as reference because this is our actual code. So remove the grid lines, remove this background color, change up this to use a color spectrum, change up these labels to be proper. If I could just remove the product label, we want to do this for all of them.
So again, I think that honestly the easiest thing to do here is to start with ChatGPT and just have that help you kind of accomplish some of your goals and then make tweaks as needed. Now I'm modifying a Plotly graph. I want to change my X axis label or from product to displaying nothing. I want my Y axis label to go from quantity ordered to quantity ordered. I want to remove the background color. I want to remove grid lines. I want to, what else do I want to do? I want to make, change the bar colors from a solid single color to a color spectrum of blues. Please implement these changes. I think that was all that we wanted to do.
Okay, that looks good. Let's copy this, paste it in, one, two, three, there we go. Let's see what happens. That looks pretty good. I really don't like this legend that popped up. Can we still modify things?
Yes. It's not showing me all my labels, that's weird.
So the big changes was this color continuous scale was added and it uses the quantity ordered color to help with that. But I don't like this legend. And I also want to be able to like replicate this stuff to move some of this stuff into like a function that I can use in these other tabs so I don't have a bunch of copied code.
So I want to make a couple additional changes. There's an annoying legend showing up in the Plotly code. Please remove that from the graph. Also standardize the code into a function because I need to apply the same styles to other Plotly charts.
Gray, I like this. Def style Plotly chart, we'll paste that up towards the top of our code and then we can copy this and put it now into our... Let's see how this works. No, the chart's still there. Okay.
So if Chat GPT doesn't work, I say go to handy dandy Google, remove legend from bar chart color spectrum Plotly express. Okay we have a couple different options. You can update the traces, update layout show legend. For the color bar, here we go. You can only use fig update layout color access show scale. So fig update and then layout underscores the same as fig.update layout and then the stuff after that color access show scale. So we can go ahead and do add to our chart over here.
Where's that function? We can add color access show scale equals false and I think that should remove it. Cool. Look at that. That looks much better.
We can add that and now we can apply this to the other ones to get those to copy the style. If we look at our final code or final dashboard, we see that we do use reds for lowest sellers and lowest sellers value. So I do want to make sure we apply that, but I think that should be a pretty simple change.
And then the only other thing I might recommend here is this is kind of annoying here. This bar, I just feel like maybe you would want to hide this. Like if you're sharing this dashboard, like they don't need to know the plotly controls that you can do here. So if we inspect this, we see we get this mode bar here, class mode bar. We can just, you know, as before you saw before, we can go ahead and add something like class mode bar display none and just hide this completely.
So now if you hover, it doesn't show up. So maybe that's something you want to do, but it's just cool to see how you can apply this HTML CSS changes to like help just clean up some things. But what did we want to do? Well, I forget what I said. Oh, we wanted to just apply these styles to the other ones. Okay.
I'm going to copy just basically this snippet of code and apply to the others. So this one uses value now instead of, I should modify the second one. Looks good. And then I guess the Y-axis label, shoot, now there's a different Y-axis label for quantity ordered. I'm going to pass this in as a, another field.
Now we've modified it to have different titles based on different things. That looks good. We can now copy this into the last two. So this one pasted in here, then instead of this blues, I think if we just did reds, it will be totally fine. I think that's also a default option. Looks good. And then finally, copy this, paste it in here, do the reds again here.
Standardizing fonts and colors
I think the final change is maybe we would want to standardize this font to be like the same blue. So I might just go back to the chat GPT window and I modify the font to be Arial size 12 with a blue color that has color, what was that hexadecimal code that we had before? It was this, and I might just make a variable at the top of our screen called like font color, just so we can standardize and just use this variable if we ever want to change fonts. Okay. Cool.
We can add this to our style Plotly chart code. Look at that. Now we got a nice change in color. Looks good. And if I wanted to just clean this up a bit, I could pass in font color to all the places that use it. You could also do the same with font type and make that Arial, set to Arial.
Styling the heat map
All right. That is the second chart. Now let's move to the heat map. What is the differences between the heat maps? With our final one, we have it all blues. So same type of process I would repeat, I would do something like, okay, where's our heat map? Okay. We've got our heat map here, and I did actually just notice an issue. This is not hour of day. This is our day, and this is order count down here. So I just need to switch the labels.
And I think in our final, we didn't actually include the title because we already have kind of the title up here. So I'm going to remove the title. Cool.
So we have this pasted into chat GPT. I have the following code. I want to have the font changed to use the color font color and use font type Arial. And I want the color spectrum to be all blues, different shades of blues. Please make changes.
So this is kind of a standard like chat GPT workflow and how I kind of access things these days. So it looks like we can pass all of this into our...I guess it makes sense to make a variable for this too. And I feel like there's kind of a...one downside of making these stylistic changes sometimes is it's hard to keep them neat as you start adding more and more things. So I'm kind of going through this quickly, but I would kind of recommend after you complete this type of stuff to kind of look through, see if there's ways you can move different aspects into different files or just clean up different details of this.
Okay. So now we have this in the blue font as well. It looks good, but it didn't use the blues. How can we get the blues? Looks like we can do SNS.lightpalette this. Let's see what happens when we do this. Looks like it works. It's not the same exact blues as I showed before. I'm curious. This is the type of difference that I'm going to just double-check what I had before. Looks like I just used what was called blues before. So I'm going to just copy this as blues, see what it does there. That looks better. I like that. Look at that.
Styling the folium heatmap
Then we also want to change these to be shades of blue. So again, just keep repeating the same type of process. I have this folium heatmap. I want to make the colors of the heatmap only shades of blues. Make modifications to the code to use this color spectrum. Okay.
That looks straightforward enough. See what happens here. I don't love the blues here. I think it's hard to see the differences. I'd rather use like the same spectrum that we used here. So I might honestly like open this up. I might highlight these different colors, like what is the max color here, like this. I might change the max color to this nice one. I might change the min color to the lightest thing here, maybe like this.
And I might now go back to chat GPT and say, I've modified the 0.2 and 1.0 of the spectrum. Please fill in the other values to be between these. I wonder if this will work. Very curious. Let's see. I feel like this is hard to...maybe, I don't know. You can play around with this a bit. Maybe it looks a little bit better. I want to just copy in whatever I had previously in my final draft. It looks like I had a 0.02, maybe that helps, I don't know. Yeah, I like the kind of lighter colors a bit more. But yeah, whatever your preference is, it doesn't matter too much. That's the heat map.
I think we have about everything. I think the only thing that we might want to change now is this bar color is slightly different. I will use this bar color. Okay. Going back to Altair graph, make the bars of the bar chart use this color.
There's a marked bar here. Let's copy this. Oh, it's already there. Just need to add the color.
All right. What happens there? Cool. And now we've got this nice color here.
I'm pretty happy with this. I think we mostly covered the different changes that we saw in our final app. That looks good.
One change I might make is first I'll commit this, get app.py, get commit, made final changes to style. And then I might also run black app.py to reformat it, reformatted with black. Cool.
Wrapping up
Well, this was some styling. This was ultimately, we built a pretty cool app. And I think that we walked through a ton of skills that you can really take and adapt to whatever you think makes sense on your own.
Really run with these skills, you know, take templates from Shiny. But I found that these Shiny dashboards are super powerful. You can build all sorts of really cool stuff and deploy them super easily. So I've been very excited playing around with Shiny and making dashboards like this.
You can build all sorts of really cool stuff and deploy them super easily.
Hopefully you found that this series was helpful. If you did enjoy this video or any videos in the series, I mean a lot of you, throw this one a thumbs up, subscribe to the channel if you haven't already. Would also be very curious what types of dashboards you would love to see in the future. So leave comments with suggestions down below.