Skip to content

Commit

Permalink
perf(streamlit): migrate from pandas to polars
Browse files Browse the repository at this point in the history
  • Loading branch information
hongbo-miao committed Dec 28, 2024
1 parent 1a1d7e1 commit 4c3df02
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ version = "1.0.0"
requires-python = "~=3.13.0"
dependencies = [
"numpy==2.2.1",
"pandas==2.2.3",
"polars==1.18.0",
"streamlit==1.41.1",
]

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,39 +2,56 @@
from datetime import datetime

import numpy as np
import pandas as pd
import polars as pl
import streamlit as st


@st.cache_data
def get_data() -> pd.DataFrame:
return pd.DataFrame(columns=["value1", "value2"])
def get_data() -> pl.DataFrame:
return pl.DataFrame(
{"timestamp": [], "value1": [], "value2": []},
schema={"timestamp": pl.Datetime, "value1": pl.Float64, "value2": pl.Float64},
)


def main() -> None:
st.title("Live Line Chart")
generator = np.random.default_rng(42)
max_data_points = 100
prev_time = None
prev_values = None
df = get_data()
placeholder = st.empty()

while True:
current_time = datetime.now()
new_data_point = (
generator.standard_normal(2) / 10.0 + df.loc[prev_time]
if prev_time
generator.standard_normal(2) / 10.0 + prev_values
if prev_values is not None
else generator.standard_normal(2)
)
df.loc[current_time] = new_data_point
prev_time = current_time
prev_values = new_data_point

# Add new row to DataFrame
df = pl.concat(
[
df,
pl.DataFrame(
{
"timestamp": [current_time],
"value1": [new_data_point[0]],
"value2": [new_data_point[1]],
}
),
]
)

# Remove old timestamps if the DataFrame exceeds the maximum size
if len(df) > max_data_points:
df = df.iloc[1:]
df = df.slice(1, len(df))

with placeholder.container():
st.header("Chart")
st.line_chart(df, height=200)
st.line_chart(df.select(["value1", "value2"]), height=200)

st.header("Table")
st.dataframe(df)
Expand Down
18 changes: 16 additions & 2 deletions machine-learning/hm-streamlit/applications/live-line-chart/uv.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 4c3df02

Please sign in to comment.