arnaudmiribel / streamlit-extras

Discover, try, install and share Streamlit re-usable bits we call "extras"!
https://extras.streamlit.app
Apache License 2.0
737 stars 125 forks source link

Styleable Container - custom font #210

Closed iuiu34 closed 9 months ago

iuiu34 commented 9 months ago

Description

Hi, is it possible to customize the font of widget with stylable_container?

code below doesn't work

    with stylable_container(
            key="comic_sans_button",
            css_styles="""
             button {
                 font-family: 'Comic Sans MS', cursive, sans-serif;
             }
             """,
    ):
        st.button("Comic Sans button")
arnaudmiribel commented 9 months ago

Hey @iuiu34,

It is! But you need to adjust the CSS path accordingly. Instead of pointing on the button itself, you need to go and point on the button's text, which is a few elements below (namely > div > p). Here's the updated code:

import streamlit as st
from streamlit_extras.stylable_container import stylable_container

with stylable_container(
        key="comic_sans_button",
        css_styles="""
         button {
            background-color: green;
            color: white;
            border-radius: 5px;
         }

         button > div > p {
             font-family: "Comic Sans MS", "Comic Sans", cursive;
         }
         """,
):
    st.button("Comic Sans button")

Demo:

image
arnaudmiribel commented 9 months ago

Btw @LukasMasuch is there a cleaner way to do this? I figured if I now added buttons before/after, while they wouldn't get the background applied, they would still use the font family. Is there a way for the font family to be only applied to the container?

image
lukasmasuch commented 9 months ago

@arnaudmiribel It should work if you add these styles in separate entries in a list, e.g.:

import streamlit as st
from streamlit_extras.stylable_container import stylable_container

with stylable_container(
        key="comic_sans_button",
        css_styles=["""
         button {
            background-color: green;
            color: white;
            border-radius: 5px;
         }
         """,
         """
         button > div > p {
             font-family: "Comic Sans MS", "Comic Sans", cursive;
         }
         """
         ],
):
    st.button("Comic Sans button")

st.button("Other button")
image

btw. nice usage of the playground :)

arnaudmiribel commented 9 months ago

Oh yea my bad, even better!