Resources

How to Perfect Your Python Dashboard with Advanced Styling! (HTML/CSS - Shiny for Python)

This is part 5 of our multi-part series on creating professional dashboards with Shiny for Python. In this video, we dive into advanced styling techniques to enhance the visual appeal and professionalism of your Shiny dashboards. We'll cover: - 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

image: thumbnail.jpg

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 HTML and CSS in Shiny

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.

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.

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.

So we see the control label now has a blue background for all of these different elements.

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.

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 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.

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.

We're going to remove this though for now, but it's good to see that this is possible.

Adding a logo and custom title

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.

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.

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.

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.

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.

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.

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.

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.

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.

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, 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.

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.

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.

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 color Zilla, 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.

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.

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. Cool. Looks like the Altair's good, so let's keep moving on.

Styling 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.

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.

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.

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.

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...

So if Chat GP 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.

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.

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.

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. Looks good.

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.

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

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.

So we have this pasted into chat GPT. 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.

So now we have this in the blue font as well. It looks good, but it didn't use 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.

Then we also want to change these to be shades of blue. So again, just keep repeating the same type of process.

I want to make the colors of the heatmap only shades of blues. Make modifications to the code to use this color spectrum.

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.

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. 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.

Final touches and wrap-up

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.

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.

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. 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. But until next time, everyone, this was fun. Peace.