Streamlit is a powerful Python library that allows data scientists and developers to create web applications quickly and easily. One of its key strengths is the ability to manage layout and customize the appearance of your app. In this blog post, we'll explore various techniques to structure your app's layout and make it visually appealing.
Streamlit provides several basic layout elements that you can use to organize your content:
Let's look at each of these in detail:
Columns allow you to create a horizontal layout for your content. Here's an example:
import streamlit as st col1, col2, col3 = st.columns(3) with col1: st.header("Column 1") st.write("This is the first column") with col2: st.header("Column 2") st.write("This is the second column") with col3: st.header("Column 3") st.write("This is the third column")
This code creates three equal-width columns. You can also specify custom widths:
col1, col2 = st.columns([2, 1])
This creates two columns, where the first column is twice as wide as the second.
Containers group related elements together. They're useful for creating logical sections in your app:
import streamlit as st container = st.container() container.write("This is inside the container") container.write("This is also inside the container") st.write("This is outside the container")
Expanders create collapsible sections, which can help reduce clutter in your app:
import streamlit as st with st.expander("Click to expand"): st.write("This content is hidden by default") st.image("https://example.com/image.jpg")
Sidebars are great for navigation menus or control panels:
import streamlit as st st.sidebar.title("Sidebar Title") option = st.sidebar.selectbox("Choose an option", ["Option 1", "Option 2", "Option 3"]) st.write(f"You selected: {option}")
You can use custom CSS to fine-tune the appearance of your Streamlit app. Here's how:
style.css
)st.markdown()
function to inject the CSS into your appimport streamlit as st def load_css(file_name): with open(file_name) as f: st.markdown(f'<style>{f.read()}</style>', unsafe_allow_html=True) load_css("style.css")
In your style.css
file, you can define custom styles:
.stButton > button { background-color: #4CAF50; color: white; } .stTextInput > div > div > input { background-color: #f1f1f1; }
To make your app responsive, you can use CSS media queries:
@media (max-width: 600px) { .stColumn { width: 100% !important; } }
This CSS will make all columns full-width on screens smaller than 600px.
You can customize charts and plots using libraries like Plotly or Altair:
import streamlit as st import plotly.express as px df = px.data.gapminder() fig = px.scatter(df, x="gdpPercap", y="lifeExp", color="continent", size="pop", hover_name="country", log_x=True, size_max=60) fig.update_layout( title="GDP per Capita vs Life Expectancy", xaxis_title="GDP per Capita", yaxis_title="Life Expectancy" ) st.plotly_chart(fig)
You can customize the appearance of Streamlit widgets using CSS classes:
import streamlit as st st.markdown(""" <style> .stSlider > div > div > div > div { background-color: #f63366; } </style> """, unsafe_allow_html=True) value = st.slider("Select a value", 0, 100, 50)
This code changes the color of the slider thumb.
By applying these techniques and best practices, you can create visually appealing and well-structured Streamlit applications that effectively communicate your data and insights.
05/10/2024 | Python
17/11/2024 | Python
21/09/2024 | Python
08/11/2024 | Python
15/10/2024 | Python
06/10/2024 | Python
05/10/2024 | Python
15/11/2024 | Python
14/11/2024 | Python
22/11/2024 | Python
17/11/2024 | Python
25/09/2024 | Python