Python の Web アプリ化 (Streamlit)2023年6月4日 | |
はじめにStreamlit は Python コードを Web アプリ化するライブラリである。それ専用の GUI ライブラリを使う。 バージョン
インストールとサンプル実行インストールは pip で行う。 >pip install streamlit サンプルを起動できる。 >streamlit hello ブラウザが起動してアプリが動作すれば OK。 ![]() アプリ例平方根計算sqrt.py
import streamlit as st
st.markdown("# 平方根計算")
text = st.text_input(value="",
label="数値",
placeholder="数値を入力")
def sqrt(a):
if a <= 0: return 0
x = 1.
for i in range(10):
x = x - (x**2 - a)/(2.*x)
return x
try:
a = float(text)
x = sqrt(a)
st.write(x)
except:
pass
次のように実行。 >streamlit run sqrt.py ![]() プロットplot.py
import streamlit as st
import streamlit.components.v1 as components
import matplotlib.pyplot as plt
import mpld3
import numpy as np
st.markdown("# プロット")
fig, ax = plt.subplots()
x = np.linspace(0, 2*np.pi, 100)
y = np.sin(3*x)
ax.plot(x, y);
#st.pyplot(fig)
fig_html = mpld3.fig_to_html(fig)
components.html(fig_html, height=600)
![]() | |
| PENGUINITIS | |