[Python] I wrote a test of "Streamlit" that makes it easy to create visualization applications.

Introduction

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.

What is 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.

Plotly is not so much, you can create a web application even in any case.

Test writing memo

Installation

$ pip install streamlit

Demo page launch

$ streamlit hello

The demo page will be launched at http: // localhost: 8501 /.

image.png

Write a script

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 /.

image.png

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.

If you really want to modify the style

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,

Remote resource 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.

Summary

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

[Python] I wrote a test of "Streamlit" that makes it easy to create visualization applications.
I tried to make a site that makes it easy to see the update information of Azure
I wrote a class that makes it easier to divide by specifying part of speech when using Mecab in python
A story that makes it easy to estimate the living area using Elasticsearch and Python
A script that makes it easy to create rich menus with the LINE Messaging API
I tried to create a list of prime numbers with python
Create a web page that runs a model that increases the resolution of the image using gradio, which makes it easy to create a web screen
A memo that I wrote a quicksort in Python
I want to create a window in Python
I tried to create a class that can easily serialize Json in Python
When I tried to create a virtual environment with Python, it didn't work
I want to create a web application that uses League of Legends data ①
A module that makes it easier to write Perl-like filter programs in Python fileinput
[Python] I tried to automatically create a daily report of YWT with Outlook mail
Quickly create a Python data analysis dashboard with Streamlit and deploy it to AWS
I wrote Python code to create a table (view) dependency diagram (PlantUML) from SQL
I wrote a program quickly to study DI with Python ①
I want to start a lot of processes from python
I tried to create a Python script to get the value of a cell in Microsoft Excel
I made a function to crop the image of python openCV, so please use it.
I made a tool that makes it convenient to set parameters for machine learning models.
I wrote a doctest in "I tried to simulate the probability of a bingo game with Python"
I wrote a script to create a Twitter Bot development environment quickly with AWS Lambda + Python 2.7
[Python3] List of sites that I referred to when I started Python
Overview of Python virtual environment and how to create it
I wrote AWS Lambda, and I was a little addicted to the default value of Python arguments
I tried to create a server environment that runs on Windows 10
I want to use a wildcard that I want to shell with Python remove
I tried to make a regular expression of "amount" using Python
A story that struggled to handle the Python package of PocketSphinx
I tried to make a regular expression of "time" using Python
[Python] A memo that I tried to get started with asyncio
Qiskit: I want to create a circuit that creates arbitrary states! !!
I tried to make a regular expression of "date" using Python
I wrote a function to load a Git extension script in Python
How to create a large amount of test data in MySQL? ??
I want to create a pipfile and reflect it in docker
I made a library that adds docstring to a Python stub file.
[python] A note that started to understand the behavior of matplotlib.pyplot
[Python] A program that rotates the contents of the list to the left
5 Ways to Create a Python Chatbot
Note that I understand the algorithm of the machine learning naive Bayes classifier. And I wrote it in Python.
I wrote a code to convert quaternions to z-y-x Euler angles in Python
[Python] I made a decorator that doesn't seem to have any use.
I made a web application in Python that converts Markdown to HTML
I tried to create a program to convert hexadecimal numbers to decimal numbers with python
[Python] A program that calculates the number of socks to be paired
[Python] I wrote a simple code that automatically generates AA (ASCII art)
I made a Discord bot in Python that translates when it reacts
Test & Debug Tips: Create a file of the specified size in Python
A memorandum of how to write pandas that I tend to forget personally
I wrote a corpus reader that reads the results of MeCab analysis
I tried to develop a Formatter that outputs Python logs in JSON
I want to color a part of an Excel string in Python
How to create a wrapper that preserves the signature of the function to wrap
[Outlook] I tried to automatically create a daily report email with Python
I made a program to check the size of a file in Python
Code reading of faker, a library that generates test data in Python
[Python] [Word] [python-docx] Try to create a template of a word sentence in Python using python-docx
I tried to create a sample to access Salesforce using Python and Bottle
I tried to verify the result of A / B test by chi-square test