[PYTHON] [Easy detonation velocity] Create a web application in minutes with Streamlit without the need for HTML files

What is Streamlit?

Streamlit is a Python open source framework.

This goal

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.

Installation

You can install it using pip.

$ pip install streamlit

Launch of demo page

$ streamlit hello 

The above command will bring up the demo screen immediately from http: // localhost: 8501. It's easy. スクリーンショット 2020-05-18 19.34.59.png

Web page creation

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]
}))

スクリーンショット 2020-05-18 22.26.06.png 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,

スクリーンショット 2020-05-18 22.48.18.png

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.

Make your app interactive with widgets

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.selectboxst.sidebar.selectbox st.checkboxst.sidebar.chechbox Then

スクリーンショット 2020-05-21 2.21.08.png

It looks like a simple web application like this.

At the end

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

[Easy detonation velocity] Create a web application in minutes with Streamlit without the need for HTML files
Publish a web application for viewing data created with Streamlit on heroku
[Python] Get the files in a folder with Python
Create a web application that recognizes numbers with a neural network
Create a local scope in Python without polluting the namespace
Create a child account for connect with Stripe in Python
Process the files in the folder in order with a shell script
Create a Twitter BOT with the GoogleAppEngine SDK for Python
Let's make a WEB application for phone book with flask Part 1
Let's make a WEB application for phone book with flask Part 2
Create a color picker for the color wheel with Python + Qt (PySide)
Let's make a WEB application for phone book with flask Part 3
Let's make a WEB application for phone book with flask Part 4
Get a list of files in a folder with python without a path
Build a web application with Django
The story of a Parking Sensor in 10 minutes with GrovePi + Starter Kit
[Introduction to Udemy Python3 + Application] 47. Process the dictionary with a for statement
I made a web application in Python that converts Markdown to HTML
[Can be done in 10 minutes] Create a local website quickly with Django
[GCP] Procedure for creating a web application with Cloud Functions (Python + Flask)
[Streamlit] I hate JavaScript, so I make a web application only with Python
Create a record with attachments in KINTONE using the Python requests module
Implement a simple application with Python full scratch without using a web framework.