forked from streamlit/streamlit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathst_dataframe_styler_support.py
110 lines (78 loc) · 2.95 KB
/
st_dataframe_styler_support.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# Copyright (c) Streamlit Inc. (2018-2022) Snowflake Inc. (2022-2024)
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import random
import numpy as np
import pandas as pd
import streamlit as st
# Explicitly seed the RNG for deterministic results
np.random.seed(0)
random.seed(0)
st.header("Pandas Styler: Value formatting")
df = pd.DataFrame({"test": [3.1423424, 3.1]})
st.dataframe(df.style.format({"test": "{:.2f}"}))
st.header("Pandas Styler: Background color")
def highlight_first(value):
return "background-color: yellow" if value == 0 else ""
df = pd.DataFrame(np.arange(0, 100, 1).reshape(10, 10))
st.dataframe(df.style.map(highlight_first))
st.header("Pandas Styler: Background and font styling")
df = pd.DataFrame(np.random.randn(20, 4), columns=["A", "B", "C", "D"])
def style_negative(v, props=""):
return props if v < 0 else None
def highlight_max(s, props=""):
return np.where(s == np.nanmax(s.values), props, "")
# Passing style values w/ all color formats to test css-style-string parsing robustness.
styled_df = df.style.map(style_negative, props="color:#FF0000;").map(
lambda v: "opacity: 20%;" if (v < 0.3) and (v > -0.3) else None
)
styled_df.apply(
highlight_max, props="color:white;background-color:rgb(255, 0, 0)", axis=0
)
styled_df.apply(
highlight_max, props="color:white;background-color:hsl(273, 98%, 60%);", axis=1
).apply(highlight_max, props="color:white;background-color:purple", axis=None)
st.dataframe(styled_df)
st.header("Pandas Styler: Gradient Styling")
weather_df = pd.DataFrame(
np.random.rand(10, 2) * 5,
index=pd.date_range(start="2021-01-01", periods=10),
columns=["Tokyo", "Beijing"],
)
def rain_condition(v):
if v < 1.75:
return "Dry"
elif v < 2.75:
return "Rain"
return "Heavy Rain"
def make_pretty(styler):
styler.set_caption("Weather Conditions")
styler.format(rain_condition)
styler.background_gradient(axis=None, vmin=1, vmax=5, cmap="YlGnBu")
return styler
styled_df = weather_df.style.pipe(make_pretty)
st.dataframe(styled_df)
st.header("LinkColumn with display value:")
st.dataframe(
pd.DataFrame(
{
"col_0": [
"https://streamlit.io",
"https://docs.streamlit.io",
"https://streamlit.io/gallery",
None,
]
}
).style.format(lambda url: url.replace("https://", "Website: ") if url else ""),
column_config={"col_0": st.column_config.LinkColumn()},
)