Skip to content

Commit 6109f04

Browse files
committed
more examples, e.g. Top10 and Excel upload
1 parent 1baca67 commit 6109f04

File tree

5 files changed

+109
-15
lines changed

5 files changed

+109
-15
lines changed

src/reports/r01_text.py

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@
2222
cols[2].subheader("Col3")
2323
cols[2].write(DUMMY_TEXT)
2424

25-
st.subheader("1/3 and 3/4 columns with only the first used")
26-
cols = st.columns([1, 3]) # 1/3 and 3/4 columns
25+
st.subheader("1/3 and 2/3 columns with only the first used")
26+
cols = st.columns((1, 2)) # 1/3 and 2/3 columns
2727
cols[0].subheader("Col1")
2828
cols[0].write(DUMMY_TEXT)
2929

@@ -36,9 +36,25 @@
3636

3737
cols[1].subheader("Markdown")
3838
cols[1].markdown("""
39-
This is a **markdown** text with *italics* and `code` and [link](https://streamlit.io).
39+
This is a markdown text with **bold**,
40+
*italics*, `code`, [link](https://streamlit.io).
4041
""")
4142

43+
st.columns(1)
44+
45+
st.subheader("Code")
46+
cols = st.columns(3)
47+
48+
s = """# Some comment
49+
print("Hello, world!")
50+
"""
51+
cols[0].write("Plain format")
52+
cols[0].code(s, language=None)
53+
cols[1].write("Python highlighting")
54+
cols[1].code(s, language="python")
55+
cols[2].write("CSV")
56+
cols[2].code("Column 1,Column 2\n12.3,23.4", language="csv")
57+
4258

4359
st.subheader("LaTeX")
4460
# 2 equals 1

src/reports/r04_tables.py

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
"""Tables."""
22

3+
import datetime as dt
4+
35
import numpy as np
46
import pandas as pd
57
import streamlit as st
@@ -20,7 +22,7 @@
2022
}
2123
)
2224

23-
st.header("simple tables")
25+
st.header("Simple Tables")
2426
st.subheader("st.write")
2527
st.write(df)
2628

@@ -45,3 +47,56 @@
4547
column_order=["url", "date", "value"],
4648
use_container_width=False,
4749
)
50+
51+
st.header("From raw data to Top 10")
52+
53+
54+
def gen_random_data(rows: int = 100) -> pd.DataFrame:
55+
"""Generate random data."""
56+
rng = np.random.default_rng()
57+
dt_offset = dt.datetime(2025, 1, 1, 0, 0, 0, tzinfo=None) # noqa: DTZ001
58+
names = ["cat 1", "cat 2", "cat 3"]
59+
data = [
60+
{
61+
"datetime": dt_offset
62+
+ dt.timedelta(seconds=int(rng.integers(0, 86400) * 31)),
63+
"category": rng.choice(names),
64+
"value": int(rng.integers(0, 100)),
65+
}
66+
for _ in range(rows)
67+
]
68+
df = pd.DataFrame(data).sort_values("datetime").reset_index(drop=True)
69+
return df
70+
71+
72+
def enrich_data(df: pd.DataFrame) -> pd.DataFrame:
73+
"""Enrich the dataframe by extracting data of datetime column."""
74+
df["year"] = df["datetime"].dt.year
75+
df["month"] = df["datetime"].dt.month
76+
df["day"] = df["datetime"].dt.day
77+
df["hour"] = df["datetime"].dt.hour
78+
df["value_rounded"] = df["value"].round(-1)
79+
return df
80+
81+
82+
df = gen_random_data()
83+
st.subheader("Raw data")
84+
st.dataframe(df, hide_index=True)
85+
86+
df = enrich_data(df)
87+
88+
st.subheader("Top10")
89+
rel_cols = st.multiselect(
90+
label="column",
91+
options=["category", "day", "hour", "value_rounded"],
92+
default=["category", "hour", "value_rounded"],
93+
)
94+
95+
if rel_cols:
96+
cols = st.columns(len(rel_cols))
97+
for i, col in enumerate(rel_cols):
98+
cols[i].write(col.title().replace("_", " "))
99+
df2 = df.groupby(col).size().to_frame("count")
100+
cols[i].dataframe(
101+
df2.head(10).sort_values(["count", col], ascending=[False, True])
102+
)

src/reports/r05_data_editor.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,6 @@
3131
df2["Lat"] = df2["Lat"].clip(lower=-180, upper=180).round(4)
3232
df2["Lng"] = df2["Lng"].clip(lower=-90, upper=90).round(4)
3333
df2 = df2.sort_values("Name")
34-
p = Path("filename.txt")
34+
p = Path("out/filename.csv")
3535
df2.to_csv(p, sep="\t", index=False, header=True, lineterminator="\n")
36-
st.rerun()
36+
st.write(f"written to `{p}`")

src/reports/r11_file_download.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@
88

99
from helper import filename_to_title, get_logger_from_filename
1010

11-
# pip install xlsxwriter
12-
1311
logger = get_logger_from_filename(__file__)
1412

1513
st.title(filename_to_title(__file__))
@@ -19,7 +17,9 @@
1917
st.write("Prepare step makes sense if calculations are needed.")
2018
cols = st.columns((1, 1, 6))
2119
if cols[0].button(label="File Prepare"):
22-
cont = """This is the content of the text file."""
20+
cont = """This is the content
21+
of the text file."""
22+
st.code(cont, language=None)
2323
buffer = io.BytesIO()
2424
buffer.write(cont.encode("utf-8"))
2525
buffer.seek(0)
@@ -44,6 +44,7 @@
4444
)
4545
cols = st.columns((1, 1, 6))
4646
if cols[0].button(label="Excel Prepare"):
47+
st.dataframe(df, hide_index=True)
4748
buffer = io.BytesIO()
4849
with pd.ExcelWriter(buffer, engine="xlsxwriter") as writer:
4950
df.to_excel(writer, sheet_name="Sheet1", index=False)

src/reports/r12_file_upload.py

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
import re
44
from io import StringIO
55

6-
import chardet # pip install chardet
6+
import chardet
7+
import pandas as pd
78
import streamlit as st
89

910
from helper import filename_to_title, get_logger_from_filename
@@ -20,17 +21,38 @@ def guess_encoding(raw_data: bytes) -> str: # noqa: D103
2021
return "utf-8" # as fallback
2122

2223

23-
st.header("Upload a text file (and guess encoding)")
24+
st.header("Upload text file (and guess encoding)")
2425

2526
st.write("set `maxUploadSize` in config.toml")
2627

27-
uploaded_file = st.file_uploader("Upload file", type="txt")
28+
uploaded_txt = st.file_uploader("Upload file", type="txt")
2829

29-
if uploaded_file:
30-
raw_data = uploaded_file.getvalue()
30+
if uploaded_txt:
31+
raw_data = uploaded_txt.getvalue()
3132
encoding = guess_encoding(raw_data)
3233
s = StringIO(raw_data.decode(encoding)).read()
3334
# some cleanup
3435
s = s.replace("\r", "")
3536
s = re.sub(r"[\n\s]+$", "", s)
36-
st.write(s.split("\n"))
37+
# st.write(s.split("\n"))
38+
st.code(s, language=None)
39+
40+
41+
st.header("Upload Excel file")
42+
st.write("this requires `pip install openpyxl`")
43+
uploaded_xlsx = st.file_uploader("Upload Excel file", type="xlsx")
44+
45+
if uploaded_xlsx:
46+
df = pd.read_excel(uploaded_xlsx, engine="openpyxl")
47+
# , sheet_name=
48+
# , usecols=
49+
st.dataframe(df, hide_index=True)
50+
51+
st.subheader("Alternative: read Excel from file")
52+
st.code(
53+
"""
54+
p = Path("path/to/local.xlsx")
55+
df = pd.read_excel(p, engine="openpyxl")
56+
""",
57+
language="python",
58+
)

0 commit comments

Comments
 (0)