9
8

More than 1 year has passed since last update.

Streamlit Sharingでシークレット情報を扱う

Last updated at Posted at 2022-01-05

StreamlitでAppを作成する際、DBのパスワードやAPI Keyなどのシークレット情報がアクセスに必要になることが多々あります。  
今回、AzureのAPIキーをStreamlit Sharingで使いたかったので、StreamlitのSecrets管理機能を試してみました。

Secretsの設定

Streamlit Sharingにてホストしたサイトの設定からSettingsを選択し、Secretsの設定を行います。
シークレット情報はTOML形式で記述します。

20220105133416.png

20220105133405.png

なお、ローカル環境でシークレットを使用したい場合は、.streamlit/secrets.tomlを作成し、同様の定義をすることで実現可能です。この際、secrets.tomlはGitリポジトリにコミットしないよう注意してください

Appでの利用方法

アプリ側で設定したシークレット情報を読み込みたい時は以下のように記述します。

import streamlit as st

KEY = st.secrets.AzureApiKey.key
ENDPOINT = st.secrets.AzureApiKey.endpoint

# 以下の書き方でも可
KEY = st.secrets["AzureApiKey"]["key"]

参考

9
8
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
9
8