streamlitをローカルで試してみる


# PythonのフルスタックWebアプリケーションのstreamlit

## streamlitとは
- https://streamlit.io/
- GitHub リポジトリ https://github.com/streamlit/streamlit
- documents https://docs.streamlit.io/
- discord https://discord.com/invite/bTz5EDYh9Z

### 少しだけ説明
- データのビジュアライズを行う
- データサイエンティスト、AIエンジニア向けの画面UI提供フレームワーク
- 簡単にデータの可視化を行うことができる
- javaScriptの知識やjavascriptのチャートライブラリ（グラフライブラリ）等は不要

## 実行方法
- Python3.8や3.9がインストールされた環境で
```
pip install streamlit

streamlit hello
```
を実行する

## ブラウザアクセス
- http://localhost:8501/

<img src ="https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/1482752/96745e50-942c-6064-4e17-0bf8e8cd8bc6.png" 
 width="500">

### 左メニューのPlotting Demoを押すと、データ作成と描画される
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/1482752/52a91620-2c5f-79db-ab30-d68b358ee332.png)
### 左メニューの Mapping Demo

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/1482752/0951e7c9-0628-dbdd-1b1d-37ddec996a62.png)


### Darkモード
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/1482752/d11ad635-7593-b119-a367-dd712ed5852c.png)

1. 右上のハンバーガーアイコン Settings
2. Themeのプルダウンで選択 ![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/1482752/6a354dfc-6ea5-ad15-e902-5aa02b8224aa.png)


## 別の導入方法

- https://www.datacamp.com/tutorial/streamlit

### pipenv をつくって作業
```sh
cd project_dir
pipenv shell
pip install streamlit
streamlit hello
```


#### コマンドの実行サンプル
```
root ➜ /workspaces/python-example $ 
root ➜ /workspaces/python-example $ cd streamlit-practice/
root ➜ /workspaces/python-example/streamlit-practice $ pipenv shell
Creating a virtualenv for this project...
Pipfile: /workspaces/python-example/streamlit-practice/Pipfile
Using /usr/local/bin/python (3.9.16) to create virtualenv...
⠇ Creating virtual environment...created virtual environment CPython3.9.16.final.0-64 in 469ms
  creator CPython3Posix(dest=/root/.local/share/virtualenvs/streamlit-practice-sagDChcu, clear=False, no_vcs_ignore=False, global=False)
  seeder FromAppData(download=False, pip=bundle, setuptools=bundle, wheel=bundle, via=copy, app_data_dir=/root/.local/share/virtualenv)
    added seed packages: pip==22.3.1, setuptools==65.6.3, wheel==0.38.4
  activators BashActivator,CShellActivator,FishActivator,NushellActivator,PowerShellActivator,PythonActivator

✔ Successfully created virtual environment!
Virtualenv location: /root/.local/share/virtualenvs/streamlit-practice-sagDChcu
Creating a Pipfile for this project...
Launching subshell in virtual environment...
 . /root/.local/share/virtualenvs/streamlit-practice-sagDChcu/bin/activate
root ➜ /workspaces/python-example/streamlit-practice $  . /root/.local/share/virtualenvs/streamlit-practice-sagDChcu/bin/activate
(streamlit-practice) root ➜ /workspaces/python-example/streamlit-practice $ 
(streamlit-practice) root ➜ /workspaces/python-example/streamlit-practice $ 
(streamlit-practice) root ➜ /workspaces/python-example/streamlit-practice $ 
(streamlit-practice) root ➜ /workspaces/python-example/streamlit-practice $ ls -latr
total 12
drwxr-xr-x 4 root root 4096 Jan 12 11:45 ..
-rw-r--r-- 1 root root  138 Jan 16 00:51 Pipfile
drwxr-xr-x 2 root root 4096 Jan 16 00:51 .
(streamlit-practice) root ➜ /workspaces/python-example/streamlit-practice $ 
(streamlit-practice) root ➜ /workspaces/python-example/streamlit-practice $ cat Pipfile 
[[source]]
url = "https://pypi.org/simple"
verify_ssl = true
name = "pypi"

[packages]

[dev-packages]

[requires]
python_version = "3.9"


(streamlit-practice) root ➜ /workspaces/python-example/streamlit-practice $ 
(streamlit-practice) root ➜ /workspaces/python-example/streamlit-practice $ pip install streamlit
Collecting streamlit
  Downloading streamlit-1.17.0-py2.py3-none-any.whl (9.3 MB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 9.3/9.3 MB 36.8 MB/s eta 0:00:00
Collecting pympler>=0.9
  Using cached Pympler-1.0.1-py3-none-any.whl (164 kB)
Collecting watchdog
  Using cached watchdog-2.2.1-py3-none-manylinux2014_x86_64.whl (78 kB)
Collecting gitpython!=3.1.19
  Using cached GitPython-3.1.30-py3-none-any.whl (184 kB)
Collecting blinker>=1.0.0
  Using cached blinker-1.5-py2.py3-none-any.whl (12 kB)
Collecting numpy
  Using cached numpy-1.24.1-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (17.3 MB)
Collecting protobuf<4,>=3.12
  Using cached protobuf-3.20.3-cp39-cp39-manylinux_2_5_x86_64.manylinux1_x86_64.whl (1.0 MB)
Collecting toml
  Using cached toml-0.10.2-py2.py3-none-any.whl (16 kB)
Collecting altair>=3.2.0
  Using cached altair-4.2.0-py3-none-any.whl (812 kB)
Collecting click>=7.0
  Using cached click-8.1.3-py3-none-any.whl (96 kB)
Collecting semver
  Using cached semver-2.13.0-py2.py3-none-any.whl (12 kB)
Collecting importlib-metadata>=1.4
  Using cached importlib_metadata-6.0.0-py3-none-any.whl (21 kB)
Collecting packaging>=14.1
  Using cached packaging-23.0-py3-none-any.whl (42 kB)
Collecting pillow>=6.2.0
  Using cached Pillow-9.4.0-cp39-cp39-manylinux_2_28_x86_64.whl (3.4 MB)
Collecting pyarrow>=4.0
  Using cached pyarrow-10.0.1-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (35.9 MB)
Collecting cachetools>=4.0
  Using cached cachetools-5.2.1-py3-none-any.whl (9.3 kB)
Collecting tzlocal>=1.1
  Using cached tzlocal-4.2-py3-none-any.whl (19 kB)
Collecting pandas>=0.21.0
  Using cached pandas-1.5.2-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (12.2 MB)
Collecting pydeck>=0.1.dev5
  Using cached pydeck-0.8.0-py2.py3-none-any.whl (4.7 MB)
Collecting tornado>=5.0
  Using cached tornado-6.2-cp37-abi3-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl (423 kB)
Collecting python-dateutil
  Using cached python_dateutil-2.8.2-py2.py3-none-any.whl (247 kB)
Collecting typing-extensions>=3.10.0.0
  Using cached typing_extensions-4.4.0-py3-none-any.whl (26 kB)
Collecting requests>=2.4
  Downloading requests-2.28.2-py3-none-any.whl (62 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 62.8/62.8 kB 15.9 MB/s eta 0:00:00
Collecting validators>=0.2
  Using cached validators-0.20.0.tar.gz (30 kB)
  Preparing metadata (setup.py) ... done
Collecting rich>=10.11.0
  Downloading rich-13.1.0-py3-none-any.whl (238 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 238.4/238.4 kB 43.9 MB/s eta 0:00:00
Collecting toolz
  Using cached toolz-0.12.0-py3-none-any.whl (55 kB)
Collecting jinja2
  Using cached Jinja2-3.1.2-py3-none-any.whl (133 kB)
Collecting jsonschema>=3.0
  Using cached jsonschema-4.17.3-py3-none-any.whl (90 kB)
Collecting entrypoints
  Using cached entrypoints-0.4-py3-none-any.whl (5.3 kB)
Collecting gitdb<5,>=4.0.1
  Using cached gitdb-4.0.10-py3-none-any.whl (62 kB)
Collecting zipp>=0.5
  Using cached zipp-3.11.0-py3-none-any.whl (6.6 kB)
Collecting pytz>=2020.1
  Downloading pytz-2022.7.1-py2.py3-none-any.whl (499 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 499.4/499.4 kB 44.2 MB/s eta 0:00:00
Collecting six>=1.5
  Using cached six-1.16.0-py2.py3-none-any.whl (11 kB)
Collecting idna<4,>=2.5
  Using cached idna-3.4-py3-none-any.whl (61 kB)
Collecting charset-normalizer<4,>=2
  Downloading charset_normalizer-3.0.1-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (198 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 198.8/198.8 kB 39.2 MB/s eta 0:00:00
Collecting certifi>=2017.4.17
  Using cached certifi-2022.12.7-py3-none-any.whl (155 kB)
Collecting urllib3<1.27,>=1.21.1
  Using cached urllib3-1.26.14-py2.py3-none-any.whl (140 kB)
Collecting commonmark<0.10.0,>=0.9.0
  Using cached commonmark-0.9.1-py2.py3-none-any.whl (51 kB)
Collecting pygments<3.0.0,>=2.6.0
  Using cached Pygments-2.14.0-py3-none-any.whl (1.1 MB)
Collecting pytz-deprecation-shim
  Using cached pytz_deprecation_shim-0.1.0.post0-py2.py3-none-any.whl (15 kB)
Collecting decorator>=3.4.0
  Using cached decorator-5.1.1-py3-none-any.whl (9.1 kB)
Collecting smmap<6,>=3.0.1
  Using cached smmap-5.0.0-py3-none-any.whl (24 kB)
Collecting MarkupSafe>=2.0
  Using cached MarkupSafe-2.1.1-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (25 kB)
Collecting attrs>=17.4.0
  Using cached attrs-22.2.0-py3-none-any.whl (60 kB)
Collecting pyrsistent!=0.17.0,!=0.17.1,!=0.17.2,>=0.14.0
  Using cached pyrsistent-0.19.3-py3-none-any.whl (57 kB)
Collecting tzdata
  Using cached tzdata-2022.7-py2.py3-none-any.whl (340 kB)
Building wheels for collected packages: validators
  Building wheel for validators (setup.py) ... done
  Created wheel for validators: filename=validators-0.20.0-py3-none-any.whl size=19581 sha256=8acc282f283006701a72892d48b363a75b95ce1726dbe877f87fa371c6c60935
  Stored in directory: /root/.cache/pip/wheels/5c/6a/fd/17a2b6f8d376e336d46414eeaf656327fd973158159b059046
Successfully built validators
Installing collected packages: pytz, commonmark, charset-normalizer, zipp, watchdog, urllib3, tzdata, typing-extensions, tornado, toolz, toml, smmap, six, semver, pyrsistent, pympler, pygments, protobuf, pillow, packaging, numpy, MarkupSafe, idna, entrypoints, decorator, click, certifi, cachetools, blinker, attrs, validators, rich, requests, pytz-deprecation-shim, python-dateutil, pyarrow, jsonschema, jinja2, importlib-metadata, gitdb, tzlocal, pydeck, pandas, gitpython, altair, streamlit
Successfully installed MarkupSafe-2.1.1 altair-4.2.0 attrs-22.2.0 blinker-1.5 cachetools-5.2.1 certifi-2022.12.7 charset-normalizer-3.0.1 click-8.1.3 commonmark-0.9.1 decorator-5.1.1 entrypoints-0.4 gitdb-4.0.10 gitpython-3.1.30 idna-3.4 importlib-metadata-6.0.0 jinja2-3.1.2 jsonschema-4.17.3 numpy-1.24.1 packaging-23.0 pandas-1.5.2 pillow-9.4.0 protobuf-3.20.3 pyarrow-10.0.1 pydeck-0.8.0 pygments-2.14.0 pympler-1.0.1 pyrsistent-0.19.3 python-dateutil-2.8.2 pytz-2022.7.1 pytz-deprecation-shim-0.1.0.post0 requests-2.28.2 rich-13.1.0 semver-2.13.0 six-1.16.0 smmap-5.0.0 streamlit-1.17.0 toml-0.10.2 toolz-0.12.0 tornado-6.2 typing-extensions-4.4.0 tzdata-2022.7 tzlocal-4.2 urllib3-1.26.14 validators-0.20.0 watchdog-2.2.1 zipp-3.11.0
(streamlit-practice) root ➜ /workspaces/python-example/streamlit-practice $ 


(streamlit-practice) root ➜ /workspaces/python-example/streamlit-practice $ streamlit hello

Collecting usage statistics. To deactivate, set browser.gatherUsageStats to False.


  Welcome to Streamlit. Check out our demo in your browser.

  Network URL: http://172.17.0.3:8502
  External URL: http://113.35.111.66:8502

  Ready to create your own Python apps super quickly?
  Head over to https://docs.streamlit.io

  May you create awesome apps!

```

## streamlitをAWS AppRunnerで動かすには？
- https://github.com/aws/apprunner-roadmap/issues/13 の要望通り、AppRunner側がWebSocketに対応していないため動かない