How to apply custom CSS styles in Streamlit apps

Data Professor · Beginner ·📊 Data Analytics & Business Intelligence ·4y ago

Key Takeaways

The video demonstrates how to apply custom CSS styles in Streamlit apps, specifically changing the background and border colors of the st.metric element. It covers activating a conda environment, modifying CSS styling, and using the st.markdown function to embed CSS code.

Full Transcript

in a prior video i've shown you how you could create a navigation bar in streamlit and you'll notice that in the st.metric that we've used the border color is blank and so in this video i'm going to show you how you could modify the border color of the st.matrix which you could use in developing your very own dashboard web application and so without further ado we're starting right now alright and so let's get started so the first thing that you want to do is you're going to fire up your terminal and then you want to activate your own contact environment and my condo environment is called data professor so i'll activate that conda activate data fester so make note that this is your own content environment and if you don't know how to activate the content environment or create your content environment i'll provide you the link to a video that i've made on everything that you need to know how to use conda and so now that we have activated it you'll notice that the name of environment is shown here and now i'm going to move to the directory where we have the files sandbox and the folder is called metric so we have two files here app.py and the style.css so the application will be stored in here and the modification to the border color will be stored in the style.css file so why don't we run the file streamlet run app.py all right and so you'll notice here that we've added the border color to be slightly dark gray and then we added a light gray as the background color so that we'll be able to make the st.metric contrast the background color of white so let me show you how it looks like before we added this customized css styling so we'll have to use the vs code here i'll move this left left part and then this will be the right of tab alright so these are the css styling that we've made so the background color here we specify it to be light gray and the border color which is at the edge here specified to be light green so let's see if we added ffff which will be white so that we'll see how it looks like before adding the color or even why don't we just just comment it okay we'll just comment it here and then we'll run it again so i have to run it from within here run it here i'll activate from here again activate environment and let's see where am i pwd is for print working directory so i'm in the current working directory so i'll type in srimlet run app.app.y okay so it looks like this so without the border color the sc metric looks like this so we'll see that there are three columns because in the code here we have three columns so the code is created on only nine lines of code so the first line will essentially be importing streamlit as st and line number three will be opening up the style.css and we'll assign it to the f variable and then we'll use the dot markdown and as input argument we're going to use the f string and inside the f string we're going to add the html code and then we're going to specify it to replace the content of f.read with the contents of style.css that we have in here and then on lines number six we're going to create three columns using the st.columns and input argument of three and for each of the column we're going to add the st.metric and so these were taken from the documentation of srimlet so let me show you trimlet docs s2.metric so what i've essentially done was copy this code and then use it as an example here okay so i'll provide you the link to that in the video description and so the ability to add border color and border background will allow you to distinguish the st.metric from the background of the app and i personally think that it also looks pretty awesome too so let me uncomment all of these save it refresh it and then we'll have the custom style here so what we've essentially done was we first have to figure out this part of the code so what is the st.metric called in the streamlit app so in order to do that you have to open up your chrome you have to click on view developer inspect elements so let me expand this a bit okay so let's have a look at the elements here so when we hover our mouse onto the st.metric column here you're going to notice that there's going to be a pop-up which will tell us that this element is called div css-1r6slb0.e1tzin5v2 so we can see that the background color here is the light gray and you'll also be able to see other aspects dimensions of the particular elements the margin padding etc and we'll notice that the other sc.metric also has the same name and so in order to modify the color we have to specify the name of the elements so we do it here and we do it inside the style.css file and then we have an opening and a closing braces and then we have the various attributes here so what we wanted to do let me close this so what we wanted to do was modify the background color so we specified here background color to be light gray we wanted to make the border color to be slightly darker gray you could also make it black if you like or any other color here if i make it black it looks like this so i just want to make it a bit subtle a bit darker right or even here how about this dark gray so you could go ahead and go to the html color codes and then you'll be able to choose your favorite color let's say that we're going to use this color that we just pasted here save it refresh it and then you have this reddish color or you can make the background to be light pink if you like find a light paint color here and then we paste the color here refresh it and there you have it you have your very own customized colors here and the border here is the width of the border here i specified to be one you can modify this to be other number as you see fit so it looks a bit darker here as it's a bit thicker and then the padding here are on the sides i specify it to be 20 to 20 70. i think i left it here oh it's redundant you could specify it to be in pixel or in percent if you like let me use percentage it looks the same and then the border radius i specify it to be 10 pixels and what if it's zero what happens have a look so you notice that it becomes a square so when the border radius is specified we'll have rounded edges like that so make it a little bit less so it's 10 save it okay you can modify the color to your own liking five all right and so that's essentially it so in order to modify the properties of this element we use the st markdown and then we embed the code from the style.css so let me know in the comments down below which aspect of srimlet do you want me to experiment with and probably hack the styling and i love to read all of your comments and so all of the codes mentioned here in this tutorial will be provided in the github repo in the video description thank you for watching until the end of this video if you made it this far drop a fire emoji in the comments down below and smash the like button subscribe if you haven't already and also hit on the notification bell so that you'll be notified of the next video and as always the best way to learn data science is to do data science and please enjoy the journey

Original Description

In this video, you will learn how to apply custom CSS styles in Streamlit apps. Particularly, you will be changing the background and border colors of the st.metric element of a Streamlit app. 👉 Code https://github.com/dataprofessor/streamlit-adjust-css Support my work: 👪 Join as Channel Member: https://www.youtube.com/channel/UCV8e2g4IWQqK71bbzGDEI4Q/join ✉️ Newsletter http://newsletter.dataprofessor.org 📖 Join Medium to Read my Blogs https://data-professor.medium.com/membership ☕ Buy me a coffee https://www.buymeacoffee.com/dataprofessor Recommended Resources 📚 Books https://kit.co/dataprofessor 😎 Taro (Tech Career Mentorship) https://www.jointaro.com/r/dataprofessor/ 📜 Google Data Analytics Professional Certificate https://imp.i384100.net/google-data-analytics 🤔 Interview Query https://www.interviewquery.com/?ref=dataprofessor 🖥️ Stock photos, graphics and videos used on this channel https://1.envato.market/c/2346717/628379/4662 Subscribe: 🌟 Coding Professor https://www.youtube.com/channel/UCJzlfIoF8nmWqJIv_iWQVRw?sub_confirmation=1 🌟 Data Professor https://www.youtube.com/dataprofessor?sub_confirmation=1 Disclaimer: Recommended books and tools are affiliate links that gives me a portion of sales at no cost to you, which will contribute to the improvement of this channel's contents. #datascience #machinelearning #dataprofessor
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from Data Professor · Data Professor · 0 of 60

← Previous Next →
1 How a Biologist became a Data Scientist
How a Biologist became a Data Scientist
Data Professor
2 WEKA Tutorial #1.1 - How to Build a Data Mining Model from Scratch
WEKA Tutorial #1.1 - How to Build a Data Mining Model from Scratch
Data Professor
3 WEKA Tutorial #1.2 - How to Build a Data Mining Model from Scratch
WEKA Tutorial #1.2 - How to Build a Data Mining Model from Scratch
Data Professor
4 WEKA Tutorial #1.3 - How to Build a Data Mining Model from Scratch
WEKA Tutorial #1.3 - How to Build a Data Mining Model from Scratch
Data Professor
5 Computational Drug Discovery: Machine Learning for Making Sense of Big Data in Drug Discovery
Computational Drug Discovery: Machine Learning for Making Sense of Big Data in Drug Discovery
Data Professor
6 Quotes #1 on Big Data and Data Science
Quotes #1 on Big Data and Data Science
Data Professor
7 Quotes #2 on Big Data and Data Science
Quotes #2 on Big Data and Data Science
Data Professor
8 Quotes #3 on Big Data and Data Science
Quotes #3 on Big Data and Data Science
Data Professor
9 Quotes #4 on Big Data and Data Science
Quotes #4 on Big Data and Data Science
Data Professor
10 Quotes #5 on Big Data and Data Science
Quotes #5 on Big Data and Data Science
Data Professor
11 Data Science 101: Starting a Data Science / Data Mining Project
Data Science 101: Starting a Data Science / Data Mining Project
Data Professor
12 Data Science 101: CRISP-DM - Data Mining / Data Science in 6 Steps
Data Science 101: CRISP-DM - Data Mining / Data Science in 6 Steps
Data Professor
13 R Programming 101: How to Define Variables
R Programming 101: How to Define Variables
Data Professor
14 R Programming 101: Read and Write CSV files
R Programming 101: Read and Write CSV files
Data Professor
15 Data Science 101: Basic Command-Line for Data Science
Data Science 101: Basic Command-Line for Data Science
Data Professor
16 Strategies for Learning Data Science in 2020 (Data Science 101)
Strategies for Learning Data Science in 2020 (Data Science 101)
Data Professor
17 Building your Data Science Portfolio with GitHub (Data Science 101)
Building your Data Science Portfolio with GitHub (Data Science 101)
Data Professor
18 R Programming 101: Setting up R programming environment (R, RStudio and RStudio.cloud)
R Programming 101: Setting up R programming environment (R, RStudio and RStudio.cloud)
Data Professor
19 Exploratory Data Analysis in R: Towards Data Understanding
Exploratory Data Analysis in R: Towards Data Understanding
Data Professor
20 Exploratory Data Analysis in R: Quick Dive into Data Visualization
Exploratory Data Analysis in R: Quick Dive into Data Visualization
Data Professor
21 Machine Learning in R: Building a Classification Model
Machine Learning in R: Building a Classification Model
Data Professor
22 Machine Learning in R: Repurpose Machine Learning Code for New Data
Machine Learning in R: Repurpose Machine Learning Code for New Data
Data Professor
23 Data Science 101: Deploying your Machine Learning Model
Data Science 101: Deploying your Machine Learning Model
Data Professor
24 Machine Learning in R: Deploy Machine Learning Model using RDS
Machine Learning in R: Deploy Machine Learning Model using RDS
Data Professor
25 Data Pre-processing in R: Handling Missing Data
Data Pre-processing in R: Handling Missing Data
Data Professor
26 Machine Learning in R: Speed up Model Building with Parallel Computing
Machine Learning in R: Speed up Model Building with Parallel Computing
Data Professor
27 Data Science 101: Overview of Machine Learning Model Building Process
Data Science 101: Overview of Machine Learning Model Building Process
Data Professor
28 Web Apps in R: Building your First Web Application in R | Shiny Tutorial Ep 1
Web Apps in R: Building your First Web Application in R | Shiny Tutorial Ep 1
Data Professor
29 Web Apps in R: Build Interactive Histogram Web Application in R | Shiny Tutorial Ep 2
Web Apps in R: Build Interactive Histogram Web Application in R | Shiny Tutorial Ep 2
Data Professor
30 Web Apps in R: Building Data-Driven Web Application in R | Shiny Tutorial Ep 3
Web Apps in R: Building Data-Driven Web Application in R | Shiny Tutorial Ep 3
Data Professor
31 Web Apps in R: Building the Machine Learning Web Application in R | Shiny Tutorial Ep 4
Web Apps in R: Building the Machine Learning Web Application in R | Shiny Tutorial Ep 4
Data Professor
32 Web Apps in R: Build BMI Calculator web application in R for health monitoring | Shiny Tutorial Ep 5
Web Apps in R: Build BMI Calculator web application in R for health monitoring | Shiny Tutorial Ep 5
Data Professor
33 Machine Learning in R: Building a Linear Regression Model
Machine Learning in R: Building a Linear Regression Model
Data Professor
34 What programming language to learn for Data Science? R versus Python
What programming language to learn for Data Science? R versus Python
Data Professor
35 How to Become a Data Scientist (Learning Path and Skill Sets Needed)
How to Become a Data Scientist (Learning Path and Skill Sets Needed)
Data Professor
36 Using Python in R
Using Python in R
Data Professor
37 Interpretable Machine Learning Models
Interpretable Machine Learning Models
Data Professor
38 Making Scatter Plots in R [Data Visualisation in R series]
Making Scatter Plots in R [Data Visualisation in R series]
Data Professor
39 Machine Learning in Python: Building a Classification Model
Machine Learning in Python: Building a Classification Model
Data Professor
40 Compare Machine Learning Classifiers in Python
Compare Machine Learning Classifiers in Python
Data Professor
41 Hyperparameter Tuning of Machine Learning Model in Python
Hyperparameter Tuning of Machine Learning Model in Python
Data Professor
42 Practical Introduction to Google Colab for Data Science
Practical Introduction to Google Colab for Data Science
Data Professor
43 File Handling in Google Colab for Data Science
File Handling in Google Colab for Data Science
Data Professor
44 Pandas for Data Science: Create and Combine DataFrames / Rename Columns
Pandas for Data Science: Create and Combine DataFrames / Rename Columns
Data Professor
45 Machine Learning in Python: Building a Linear Regression Model
Machine Learning in Python: Building a Linear Regression Model
Data Professor
46 Machine Learning in Python: Principal Component Analysis (PCA) for Handling High-Dimensional Data
Machine Learning in Python: Principal Component Analysis (PCA) for Handling High-Dimensional Data
Data Professor
47 How to Plot an ROC Curve in Python | Machine Learning in Python
How to Plot an ROC Curve in Python | Machine Learning in Python
Data Professor
48 Installing conda on Google Colab for Data Science
Installing conda on Google Colab for Data Science
Data Professor
49 Use native R on Google Colab for Data Science
Use native R on Google Colab for Data Science
Data Professor
50 How to Save and Download files from Google Colab
How to Save and Download files from Google Colab
Data Professor
51 Easy Web Scraping in Python using Pandas for Data Science
Easy Web Scraping in Python using Pandas for Data Science
Data Professor
52 Data Science for Computational Drug Discovery using Python (Part 1)
Data Science for Computational Drug Discovery using Python (Part 1)
Data Professor
53 Pandas Profiling for Data Science (Quick and Easy Exploratory Data Analysis)
Pandas Profiling for Data Science (Quick and Easy Exploratory Data Analysis)
Data Professor
54 Exploratory Data Analysis in Python using pandas
Exploratory Data Analysis in Python using pandas
Data Professor
55 Quick tour of PyCaret (a low-code machine learning library in Python)
Quick tour of PyCaret (a low-code machine learning library in Python)
Data Professor
56 How to Upload Files to Google Colab
How to Upload Files to Google Colab
Data Professor
57 How to Install and Use Pandas Profiling on Google Colab
How to Install and Use Pandas Profiling on Google Colab
Data Professor
58 How to Adjust the Style of Pandas DataFrame
How to Adjust the Style of Pandas DataFrame
Data Professor
59 How to use Bamboolib for Data Wrangling in Data Science
How to use Bamboolib for Data Wrangling in Data Science
Data Professor
60 How to use Pandas Profiling on Kaggle
How to use Pandas Profiling on Kaggle
Data Professor

This video teaches how to customize Streamlit apps by applying CSS styles to the st.metric element, allowing for personalized dashboard web applications. It covers the necessary steps and code to achieve this customization.

Key Takeaways
  1. Activate a conda environment
  2. Modify CSS styling in the style.css file
  3. Use the st.markdown function to embed CSS code
  4. Specify the element name in the CSS file
  5. Modify attributes such as background color, border color, and border width
💡 The st.markdown function can be used to embed CSS code and customize the appearance of Streamlit apps.

Related Reads

📰
Classroom vs Online Data Science Training in Hyderabad | Coding MastersClassroom vs Online Data…
Learn why data science is in high demand and how to get trained in Hyderabad, whether through classroom or online modes, to boost your career
Medium · Data Science
📰
SciDraw Alternative: Paper Banana for Scientific Figures
Discover Paper Banana as a SciDraw alternative for creating scientific figures, and learn how to use it for your data visualization needs
Medium · Data Science
📰
“Missão: Risco da Carteira”.
Learn to assess portfolio risk using data science techniques and tools
Medium · Data Science
📰
When Ten Analysts Agree and Two Are Right — What Numbers 13 Knew About Groupthink
Learn how to identify and avoid groupthink in analysis and decision-making, and why it's crucial for accurate threat assessments
Medium · AI
Up next
This could be the most perfect data frontend
Matt Williams
Watch →