ゆとり世代の自由研究

勉強が一生終わりません

Streamlitの基本

・実行コマンド

streamlit run your_script.py [-- script args]

 

・st.write

import streamlit as st
import pandas as pd

st.write("Here's our first attempt at using data to create a table:")
st.write(pd.DataFrame({
    'first column': [1, 2, 3, 4],
    'second column': [10, 20, 30, 40]
}))

 

・折れ線グラフを描く

import streamlit as st
import numpy as np
import pandas as pd

chart_data = pd.DataFrame(
     np.random.randn(20, 3),
     columns=['a', 'b', 'c'])

st.line_chart(chart_data)

 

・スライダー

import streamlit as st
x = st.slider('x')  # 👈 this is a widget
st.write(x, 'squared is', x * x)

 

・文字の入力

import streamlit as st
st.text_input("Your name", key="name")

# You can access the value at any point with:
st.session_state.name

 

・サイドバー

import streamlit as st

# Add a selectbox to the sidebar:
add_selectbox = st.sidebar.selectbox(
    'How would you like to be contacted?',
    ('Email', 'Home phone', 'Mobile phone')
)

# Add a slider to the sidebar:
add_slider = st.sidebar.slider(
    'Select a range of values',
    0.0, 100.0, (25.0, 75.0)
)

 

・タイトル

st.title('Title Of This App')

 

ヒストグラム

hist_values = np.histogram(
    data[DATE_COLUMN].dt.hour, bins=24, range=(0,24))[0]
st.bar_chart(hist_values)

 

・steamli cloudでの公開

  1. アプリをパブリック GitHub リポジトリに置きます (そして、requirements.txt があることを確認してください)。
  2. share.streamlit.ioにサインインします 
  3. 「アプリのデプロイ」をクリックし、GitHub URL を貼り付けます。