Summary
I’m coming from the Plotly Dash world and am attempting to take advantage of Streamlit’s much better “datepicker” widget for a simple web app.
Currently, I am attempting to make a Streamlit version of my Plotly Dash app that behaves and looks the same. The behavior is fine, it’s the styling that has proven challenging. Specifically, I am trying to find a way to set the styling of the Streamlit st.plotly_chart
such that the resulting figure dynamically adjusts to fill the vertical space of the viewport (via something like height: 100vh
).
Here’s an image of the apps side by side with Dash on the left, Streamlit on the right with code bellow.
Example Code Comparison
Plotly Dash (left in image above)
import dash
from dash import dcc, html
import plotly.graph_objs as go
test_fig = go.Figure(
data=[
go.Scatter(
x=[1, 2, 3, 4, 5, 6],
y=[9, 8, 7, 6, 5, 4],
mode='markers+lines',
name='test'
)
]
)
app = dash.Dash(__name__, title="Plotly Dash Example")
app.layout = html.Div([
# date picker
dcc.DatePickerRange(),
# Figure
dcc.Graph(
figure=test_fig,
style={'height': '92vh'}, # How can I do this in Streamlit?
),
])
if __name__ == '__main__':
# run the Dash app
app.run_server(debug=False)
Streamlit (right in image above)
import streamlit as st
import plotly.graph_objs as go
test_fig = go.Figure(
data=[
go.Scatter(
x=[1, 2, 3, 4, 5, 6],
y=[9, 8, 7, 6, 5, 4],
mode='markers+lines',
name='test'
)
]
)
# create streamlit app
st.set_page_config(layout='wide')
# This section was copy pasted from other forums to reduce white space and hide header and footer
st.markdown("""
<style>
.css-18e3th9 {
padding-top: 0rem;
padding-bottom: 0rem;
padding-left: 5rem;
padding-right: 5rem;
}
.css-1d391kg {
padding-top: 0rem;
padding-right: 1rem;
padding-bottom: 0rem;
padding-left: 1rem;
}
#MainMenu {visibility: hidden;}
footer {visibility: hidden;}
</style>
""", unsafe_allow_html=True)
date_range = st.date_input(label='Select a date range')
st.plotly_chart(test_fig, use_container_width=True)
Dash provides the ability to easily set the div height, but I’m having a hard time looking through the resulting Streamlit html and css to understand what I could do with st.markdown
to get the same look.
System details
- Streamlit version: 1.16.0
- Python version: 3.10
- venv virtual environment
Thanks in advance!