Streamlit
is a Python open source framework.
For the time being, I would like to easily create something that looks like a web application. The troublesome thing in creating a web application is various backend processing and HTML file creation, but Streamlit seems to have no need to do those tasks at all. You can create a web page with just one Python file.
You can install it using pip.
$ pip install streamlit
$ streamlit hello
The above command will bring up the demo screen immediately from http: // localhost: 8501. It's easy.
Create a Python file in a directory of your choice. The file name can be anything, but this time it will be officially first_app.py
.
$ touch first_app.py
$ cd first_app.py
Then write to this file. Since it is the very first, let's display Hello world.
first_app.py
import streamlit as st
st.title('MyApp')
st.write("HelloWorld")
Execute the following command on the directory where first_app.py
is located
$ streamlit run first_app.py
You probably see Hello world on your web page. It's a little touching to be able to create a web page with such a small amount of code without writing HTML or CSS files. You can also easily display headers and data frames.
first_app.py
import streamlit as st
import numpy as np
import pandas as pd
st.title("HelloWorld")
st.subheader("This is subheader")
st.write("This is testdata")
st.write(pd.DataFrame({
'first column': [1, 2, 3, 4],
'second column': [10, 20, 30, 40]
}))
As you can see, Streamlit provides various methods such as st.write ()
and st.title ()
.
See the official documentation (https://docs.streamlit.io/api.html#display-text) for other methods.
magic commands This alone is convenient enough because the front processing can be done without the need for an HTML file. What a streamlit actually has more groundbreaking commands.
first_app.py
import streamlit as st
import numpy as np
import pandas as pd
"""
# My first app
### This is subheader
This is testdata
"""
st.write(pd.DataFrame({
'first column': [1, 2, 3, 4],
'second column': [10, 20, 30, 40]
}))
If you write like this and check the application,
Oh ... awesome Streamlit cluttered in the code, read the comments well, It will write to the app. Officially it's called a magic command. It's just magic By the way, it doesn't seem to work unless it's Python 3 or later.
You can easily add checkboxes and select boxes. As a test, select Tokyo or Osaka in the select box and scatter the dots on the map so that you can hit it. And if you check the check box, you can see the list of coordinate data.
locate = {"Tokyo":[35.68, 139.76] , "Osaka":[34.70 ,135.49]}
#Select box
l = st.selectbox(
'Which places do you like best?',
("Tokyo","Osaka"))
'You selected:', l
#Plot the map
map_data = pd.DataFrame(
np.random.randn(1000, 2) / [50, 50] + locate[l],
columns=['lat', 'lon'])
st.map(map_data)
#Checkbox
if st.checkbox('Show dataframe'):
st.write(map_data)
Also, most commands can be moved to the left sidebar by adding st.sideber
.
st.selectbox
→st.sidebar.selectbox
st.checkbox
→st.sidebar.chechbox
Then
It looks like a simple web application like this.
I was able to create a web application with just one Python file and a few lines of code.
I felt that Django
and Dash
were fast enough in the same Python, but Streamlit
was explosive.
Besides, I could embed graphs, images, videos, etc. with a simple line of code.
If you are not so particular about the design and want to publish something that you have developed quickly, I think it is worth trying.
Recommended Posts