VSCode+Docker上でStreamlit開発環境を構築すること目標とします。
VSCodeのRemote – Containersエクステンションを使うことで、ブレークポイントを張ってデバッグできますので大変便利です。
環境
Windows 10 Pro
Docker Desktop 4.1.1
Visual Studio Code 1.62.2
手順概要
- 作業用フォルダの作成
- Dockerfile作成
- コンテナでフォルダを開く
- Streamlitデモプログラムの起動
- Streamlitプログラムの開発
作業用フォルダの作成
作業用のフォルダを作成し、Visual Studio Codeで開きます。
今回は、D:\Streamlitを作業用フォルダとします。
![](https://shanch.azurewebsites.net/wp-content/uploads/2022/01/11/image-25.png)
![](https://shanch.azurewebsites.net/wp-content/uploads/2022/01/11/image-26.png)
以下のようなメッセージが出てきたときは、Yes, I trust the authorsを選択します。
![](https://shanch.azurewebsites.net/wp-content/uploads/2022/01/11/image-24.png)
Dockerfile作成
作業用フォルダをVisual Studio Codeで開いたら、New Fileアイコンをクリックし、Dockerfileというファイルを作ります。
![](https://shanch.azurewebsites.net/wp-content/uploads/2022/01/11/image-27.png)
![](https://shanch.azurewebsites.net/wp-content/uploads/2022/01/11/image-28.png)
以下のコードをDockerfileに入力し、保存します。
FROM python:3.7.4
RUN pip install streamlit
![](https://shanch.azurewebsites.net/wp-content/uploads/2022/01/11/image-32.png)
コンテナでフォルダを開く
Visual Studio CodeにRemote – Containersエクステンションをインストール します。
すでにインストール済の場合は、この手順をスキップして先に進んでください。
画面左端のExtensionsアイコンをクリックし、Remote – Containersを検索し、Installします。
![](https://shanch.azurewebsites.net/wp-content/uploads/2022/01/11/image-29.png)
インストールが完了すると、画面左下に緑のアイコンが出てくるので、それをクリックします。
![](https://shanch.azurewebsites.net/wp-content/uploads/2022/01/11/image-30.png)
画面上部のコマンドパレットからReopen in Containerを選択します。
![](https://shanch.azurewebsites.net/wp-content/uploads/2022/01/11/image-31-1024x516.png)
続けてFrom ‘Dockerfile’を選択します。
![](https://shanch.azurewebsites.net/wp-content/uploads/2022/01/11/image-33.png)
Streamlitデモプログラムの起動
コンテナでフォルダが開かれたら、画面下部のTerminalで以下のコマンドを入力し、Enterを押します。
streamlit hello
![](https://shanch.azurewebsites.net/wp-content/uploads/2022/01/11/image-34.png)
Streamlitが起動したら、Open in Browserをクリックします。
![](https://shanch.azurewebsites.net/wp-content/uploads/2022/01/11/image-35-1024x268.png)
Streamlitのデモ画面がブラウザで見れれば環境構築は完了です。
![](https://shanch.azurewebsites.net/wp-content/uploads/2022/01/11/image-36-1024x503.png)
Ctrl+Cでデモアプリを止めます。
Streamlitプログラムの開発
最後にHello World!とだけ表示する簡単なプログラムの開発をしてみます。
app.pyというファイルを追加し、ファイルの中身を以下のようにして保存します。
import streamlit as st
def helloworld():
st.header("Hello World!")
if __name__ == "__main__":
helloworld()
Terminalに以下のコマンドを打ってEnterを押します。
streamlit run app.py
ブラウザでhttp://localhost:8501にアクセスし、以下のように表示されることを確認します。
![](https://shanch.azurewebsites.net/wp-content/uploads/2022/01/12/image.png)
Ctlr+Cでいったん終了します。
次に、ブレークポイントを張ってデバッグしてみます。
ブレークポイントを張るためにはPythonエクステンションが必要なので、これをまずインストールします。
Docker環境にインストールする必要がありますので、ホストPCのVSCodeにPythonエクステンションが入っていても、この作業が必要です。
![](https://shanch.azurewebsites.net/wp-content/uploads/2022/01/12/image-1-1024x503.png)
Pythonエクステンションのインストールが終わったら、app.pyを開き、ブレークポイントを置きたいところの行番号の少し左をクリックします。
ブレークポイントが設定されると、赤いマークがつきます。
赤いマークをクリックすると、ブレークポイントを外すことができます。
![](https://shanch.azurewebsites.net/wp-content/uploads/2022/01/12/image-2.png)
app.pyを開いた状態で、画面左のRun and Debugアイコンをクリックします。
![](https://shanch.azurewebsites.net/wp-content/uploads/2022/01/12/image-3.png)
Run and Debugボタンをクリックし、画面上部中央のコマンドパレットのPython Fileを選択します。
![](https://shanch.azurewebsites.net/wp-content/uploads/2022/01/12/image-4-1024x338.png)
プログラムがスタートし、ブレークポイントを設定した行で止まることが確認できます。
![](https://shanch.azurewebsites.net/wp-content/uploads/2022/01/12/image-5-1024x346.png)
今回はデバッグの必要がない単純なプログラムでしたが、複雑なプログラムをデバッグするときには大変便利な機能ですので、ぜひお試しください!