Py.Cafe

maartenbreddels/

streamlit-custom-metrics

Streamlit Custom Metrics Visualization

DocsPricing
  • app.py
  • requirements.txt
app.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
import streamlit as st


def my_metric(label, value, bg_color, icon="fas fa-asterisk"):
    fontsize = 18
    valign = "left"    
    lnk = '<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.12.1/css/all.css" crossorigin="anonymous">'

    bg_color_css = f'rgb({bg_color[0]}, {bg_color[1]}, {bg_color[2]}, 0.75)'

    htmlstr = f"""<p style='background-color: {bg_color_css}; 
                            font-size: {fontsize}px; 
                            border-radius: 7px; 
                            padding-left: 12px; 
                            padding-top: 18px; 
                            padding-bottom: 18px; 
                            line-height:25px;'>
                            <i class='{icon} fa-xs'></i> {value}
                            </style><BR><span style='font-size: 14px; 
                            margin-top: 0;'>{label}</style></span></p>"""

    st.markdown(lnk + htmlstr, unsafe_allow_html=True)


green = (0, 204, 102)
red = (204, 0, 102)
icon_error = "fas fa-bug"
icon_observation = "fas fa-asterisk"

my_metric("Observations", 123, green)
my_metric("Errors", 13, red, icon_error)


st.markdown("# In columns")

col1, col2 = st.columns(2)
with col1:
    my_metric("Observations", 123, green, icon_observation)
with col2:
    my_metric("Errors", 13, red, icon_error)