[PYTHON] Creating a data analysis application using Streamlit

What is Streamlit

This article explains how to use Streamlit. Streamlit is a python framework that allows you to create front-end applications. Graphs created with Pandas DataFrame and drawing libraries such as plotly and altair can be embedded, and can be applied to data analysis depending on the device.

environment

Installation

It can be installed in several ways.

  1. Install with pip. Official HP Recommended

Terminal


pip install streamlit
  1. Build a virtual environment with conda. Refer to the official forum

Terminal


conda create -y -n streamlit python=3.7
conda activate streamlit
pip install streamlit

File execution

Execute the Python file as follows.

Terminal


streamlit run filename.py

Streamlit features

The official HP API reference summarizes the features of streamlit. Among them, the functions that can be used for data analysis are summarized.

Display title

st_app.py


import streamlit as st
st.title('My app')

スクリーンショット 2020-10-16 23.51.02.png

View document

st_app.py


import streamlit as st
st.write("Good morning")

スクリーンショット 2020-10-16 23.51.14.png

Displaying DataFrame

st_app.py


import streamlit as st
import pandas as pd
st.table(pd.DataFrame({
    'first column': [1, 2, 3, 4],
    'second column': [10, 20, 30, 40]
}))

スクリーンショット 2020-10-16 23.51.22.png

Markdown display

st_app.py


import streamlit as st
st.markdown('# Markdown documents')

View Plotly

Plotly is one of the python drawing libraries that can create various types of graphs.

st_app.py


import streamlit as st
import plotly.graph_objs as go

animals = ['giraffes', 'orangutans', 'monkeys']
populations = [20, 14, 23]

fig = go.Figure(data=[go.Bar(x=animals, y=populations)])

fig.update_layout(
    xaxis = dict(
        tickangle = 0,
        title_text = "Animal",
        title_font = {"size": 20},
        title_standoff = 25),
    yaxis = dict(
        title_text = "Populations",
        title_standoff = 25),
    title ='Title')


st.plotly_chart(fig, use_container_width=True)

スクリーンショット 2020-10-16 22.42.45.png

Display of Altair

Altair is one of the python drawing libraries that can create various types of graphs. It is characterized by inputting data with Pandas DataFrame. Reference

st_app.py


import streamlit as st
import altair as alt
from vega_datasets import data

source = data.cars()

fig = alt.Chart(source).mark_circle(size=60).encode(
    x='Horsepower',
    y='Miles_per_Gallon',
    color='Origin',
    tooltip=['Name', 'Origin', 'Horsepower', 'Miles_per_Gallon']
).properties(
    width=500,
    height=500
).interactive()

st.write(fig)

スクリーンショット 2020-10-16 22.56.31.png

Button display

Altair can display a button that returns a bool type.

st_app.py


import streamlit as st
answer = st.button('Say hello')

if answer == True:
     st.write('Why hello there')
else:
     st.write('Goodbye')

[Before pressing] スクリーンショット 2020-10-19 10.00.35.png

[After pressing] スクリーンショット 2020-10-19 10.03.34.png

Check button display

It is also possible to display a check button that returns a bool type.

st_app.py


import streamlit as st
answer = st.button('Say hello')

if answer == True:
     st.write('Why hello there')
else:
     st.write('Goodbye')

【no check】 スクリーンショット 2020-10-19 10.09.26.png

[Checked] スクリーンショット 2020-10-19 10.11.17.png

Radio button display

It is also possible to select an element with a radio button.

st_app.py


import streamlit as st
genre = st.radio(
     "What's your favorite movie genre",
     ('Comedy', 'Drama', 'Documentary'))

if genre == 'Comedy':
     st.write('You selected comedy.')
else:
     st.write("You didn't select comedy.")

スクリーンショット 2020-10-19 10.15.51.png

Dropdown display

If you want to select only one from the dropdown:

st_app.py


import streamlit as st
agree = st.checkbox('I agree')

if agree == True :
     st.write('Great!')

スクリーンショット 2020-10-16 23.46.55.png

To select two or more from the dropdown at the same time:

st_app.py


import streamlit as st
options = st.multiselect(
    'What are your favorite colors',
    ['Green', 'Yellow', 'Red', 'Blue'],
    ['Yellow', 'Red'])

st.table(options)

スクリーンショット 2020-10-16 23.47.16.png

Slider display

To select one value: In the following example, a slider that moves with a minimum value of 0, a maximum value of 130, an interval of 1, and an initial value of 25 is displayed.

st_app.py


import streamlit as st
age = st.slider('How old are you?',  min_value=0, max_value=130, step=1, value=25)
st.write("I'm ", age, 'years old')

スクリーンショット 2020-10-16 23.14.24.png

To select two values: In the following example, a slider that moves with a minimum value of 0.0, a maximum value of 100.0, and an initial value of (25.0, 75.0) is displayed.

st_app.py


import streamlit as st
values = st.slider(
    'Select a range of values',
   0.0, 100.0, (25.0, 75.0))
st.write('Values:', values)

スクリーンショット 2020-10-16 23.14.34.png

Application example

Recommended Posts

Creating a data analysis application using Streamlit
Creating a web application using Flask ①
Creating a web application using Flask ③
Creating a web application using Flask ④
Data analysis using xarray
Data analysis using Python 0
Creating an interactive application using a topic model
Data analysis using python pandas
Creating a simple table using prettytable
Recommendation of data analysis using MessagePack
Creating a learning model using MNIST
Creating Google Spreadsheet using Python / Google Data API
Let's turn PES data analysis software into a web application! Second step!
I tried to perform a cluster analysis of customers using purchasing data
Publish a web application for viewing data created with Streamlit on heroku
Prepare a programming language environment for data analysis
Data analysis Titanic 2
Creating training data
Data analysis python
How to deploy a Streamlit application to GCP (GAE)
Create a binary data parser using Kaitai Struct
Data analysis Titanic 1
Data analysis in Python: A note about line_profiler
Data analysis Titanic 3
A well-prepared record of data analysis in Python
Practice of creating a data analysis platform with BigQuery and Cloud DataFlow (data processing)
A story about data analysis by machine learning
Quickly create a Python data analysis dashboard with Streamlit and deploy it to AWS
Create a shogi game record management application using Django 5 ~ Pass DB data to Template ~
Create a data collection bot in Python using Selenium
100 language processing knock-92 (using Gensim): application to analogy data
[Python] [Word] [python-docx] Simple analysis of diff data using python
Try creating a compressed file using Python and zlib
Big data analysis using the data flow control framework Luigi
Creating a graph using the plotly button and slider
I tried reading data from a file using Node.js.
[Technical book] Introduction to data analysis using Python -1 Chapter Introduction-
Data analysis parts collection
[Day 9] Creating a model
Creating a Home screen
4. Creating a structured program
Visualize data with Streamlit
Data analysis overview python
Data cleansing 2 Data cleansing using DataFrame
Data cleaning using Python
Creating a scraping tool
Python data analysis template
Web application using Bottle (1)
Creating a dataset loader
Orthologous analysis using OrthoFinder
Data analysis with Python
Build a data analysis environment with Kedro + MLflow + Github Actions
100 Language Processing Knock-84 (using pandas): Creating a word context matrix
A memo when creating a directed graph using Graphviz in Python
Dockerfile for creating a data science environment based on pip3
[Treasure Data] [Python] Execute a query on Treasure Data using TD Client
Build a python data analysis environment on Mac (El Capitan)
[Introduction] Artificial satellite data analysis using Python (Google Colab environment)
<Python> Build a dedicated server for Jupyter Notebook data analysis
I tried to analyze scRNA-seq data using Topological Data Analysis (TDA)
The story of creating a database using the Google Analytics API