When creating a web application with graphs based on Python, I have used the combination of the graph library Plotly
+ web application framework Dash by Plotly
.
However, this time I met a large rookie Streamlit
with great potential to threaten the position of Dash
, so I will drop a memo written as a test.
Streamlit
?Streamlit is an open-source app framework for Machine Learning and Data Science teams. Create beautiful data apps in hours, not weeks. All in pure Python. All for free.
An application framework that allows you to create a web application with a single Python script without writing HTML / CSS / JavaScript.
The front end is made with React
+ Bootstrap
, and it can be said that it is almost the same type of framework as Dash
, including the point that the Python script alone is OK.
Compared to Dash
, Streamlit
does not have the flexibility to freely decide the style and page layout.
There is no API to put custom CSS and style, and the page layout is firmly decided by the side menu + body template.
In other words, since the framework side defines the specifications including the design, you can focus more than Dash
on writing scripts for the main processing of data processing and analysis without the cost of designing.
Also, Dash
is a web application framework dedicated to Plotly
, but Streamlit
supports the following graph libraries.
Matplotlib
--Seaborn
is also a wrapper for Maptplotlib
, so it's probably okayAltair
Graphviz
Plotly
――Of course, Plotly Express
is okay too!Bokeh
Plotly
is not so much, you can create a web application even in any case.
$ pip install streamlit
$ streamlit hello
The demo page will be launched at http: // localhost: 8501 /
.
Write a sample script qiita.py
that plots the data for Plotly Express
. It's pretty simple.
qiita.py
import streamlit as st
import plotly.express as px
import plotly.io as pio
# data
data = px.data.iris()
# sidemenu
st.sidebar.markdown(
"# Qiita sample"
)
template = st.sidebar.selectbox(
"Template", list(pio.templates.keys())
)
# body
st.write(
px.scatter(data, x="sepal_width", y="sepal_length", template=template)
)
On the directory where qiita.py
is located
$ streamlit run qiita.py
Will launch the application at http: // localhost: 8501 /
.
If you write in Dash
, you can write HTML components in a hurry, and I think that the amount of code will be doubled.
Tips
I will update it from time to time.
Create a markdown component with st.markdown
and write the<style>
tag directly while giving true
to the ʻunsafe_allow_html` parameter.
st.markdown(
"<style>h1{color: red}</style>",
unsafe_allow_html=True
)
It's a black magic code because Streamlit
doesn't have a styling mouth, but even if you check the forum page of the main body, it seems that there is only this much,
streamlit run
For example, if you want to execute the source code uploaded to GitHub quickly, Streamlit
can be executed as follows without a local clone.
$ streamlit run https://raw.githubusercontent.com/prs-watch/streamlit-sample/master/sample.py
The sample repository illustrated above is here.
Since the cost of designing can be cut down, you can create a web application quickly with a small amount of code. The design provided is simple and I like it.
Dash
has the good points of Dash
, and Streamlit
itself has not been released in the major version, but it is quite useful if you want to create an application easily.
The recommendation level is high, so if you want to make a dashboard, for example, why not write a test once?
Recommended Posts