Bye Bye Moore


データサイエンスに向いたPython用可視化ツールstreamlit その5:表示をカラム分けする


import streamlit as st
import sqlite3
import pandas as pd
import as px
from datetime import datetime, timedelta

# Connect to SQLite database
con = sqlite3.connect("logging_data.db")

# Get data from loggingData table
df = pd.read_sql_query("SELECT * FROM loggingData", con)
df['記録日時'] = pd.to_datetime(df['記録日時'])

# Streamlit app
st.title("Logging Data Visualization")

# Slider for date range
today =
start_date = st.slider("Start date", min_value=today - timedelta(days=100), max_value=today - timedelta(days=1), value=today - timedelta(days=100), step=timedelta(days=1))
end_date = st.slider("End date", min_value=start_date + timedelta(days=1), max_value=today, value=today, step=timedelta(days=1))

# Filter data by date range
filtered_df = df[(df['記録日時'] >= start_date) & (df['記録日時'] <= end_date)]

# Plot data
fig = px.line(filtered_df, x='記録日時', y='測定値', color='記録装置ID', labels={'記録日時': 'Date', '測定値': 'Measurement Value'})

# Find the highest measurement value for each device
devices = ["A001", "A002", "B001"]
metrics = []

for device in devices:
    device_data = filtered_df[filtered_df["記録装置ID"] == device]
    highest_measurement = device_data.loc[device_data["測定値"].idxmax()]
    metrics.append((device, highest_measurement["記録日時"], highest_measurement["測定値"]))

# Display highest measurement value using st.metric
cols = st.columns(3)
for i, (device, date, value) in enumerate(metrics):
    cols[i].metric(label=f"Device {device}", value=value, delta=date.strftime("%Y-%m-%d %H:%M:%S"))

# Close SQLite connection