1
4

More than 1 year has passed since last update.

pythonでsqliteを使ってstreamlitに列と数字を条件に表示

Last updated at Posted at 2023-08-16

pythonでsqliteを使ってstreamlitに列と数字を条件にデータフレームで表示

import numpy as np
import pandas as pd
import sqlite3
import seaborn as sns
!pip install pycaret
!pip install streamlit

from pycaret.datasets import get_data
#ボストンデータを取得
df = get_data('boston')

# データベース名とテーブル名
db_name = 'datasets.db'
table_name = 'tips'

# SQLiteに書き込む
with sqlite3.connect(db_name) as conn:
    df.to_sql(table_name, conn)
app_training.py
import streamlit as st
import sqlite3
import pandas as pd

#先ほど設定したDBの名前
db_name = 'datasets.db'

select_all_sql = 'select ' + '*' + ' from ' + 'tips'
with sqlite3.connect(db_name) as conn:
    df_from_sql = pd.read_sql(select_all_sql, conn)

#列名を取り出す
df_from_sql_columns = df_from_sql.columns

#####stゾーン開始

option = st.selectbox(
    'どの列がはいっているデータを抽出しますか?',
    (df_from_sql_columns)
)

num = st.text_input('数字を半角で入力してください :例 0.04')
#列と入力値両方が入力されるまで待つためのif文
if not option or not num:
  st.title("列と数字が入力されるまで待ちます")
else:
  st.title("選択されました")
  def sql1(columns, num2):
    sql = 'select ' + '*' + ' from ' + 'tips' + ' where ' + columns + ' > ' + num2
    return sql
  sql2 = sql1(option, num)
  with sqlite3.connect(db_name) as conn:
      df_from_sql2 = pd.read_sql(sql2, conn)
  st.dataframe(df_from_sql2)
#####stゾーン終了
streamlit run app_training.py

streamlit.jpg

1
4
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
1
4