Це відео не доступне.
Перепрошуємо.

Custom Streamlit Background Image/Color Gradient through CSS

Поділитися
Вставка
  • Опубліковано 17 сер 2024
  • You can change the ‪@streamlitofficial‬ background color using Streamlit theming, but how do you go putting a color gradient or an external image?
    In this video, I show you how to use the CSS Markdown Trick to inject your own custom background. I will first use the st.markdown(..., unsafe_allow_html=True) method to inject a CSS Gradient Color pattern, and show you problems you'll have if you try to further edit it.
    I then proceed to load an image from an URL to Streamlit's background.
    Finally, I fail to load a local image as a Streamlit background using an URL, and instead inject the image to CSS as a base64 encoded text.
    I conclude by adding some CSS background properties to reposition, resize and have the background image scroll with the app.
    This gives you the necessary tools to customize your Streamlit app background to your heart’s desire through CSS.
    📧 My email list to keep up to date with the latest news, tutorials and resources about Streamlit & friends ➡ andfanilo-news...
    💰 Buy me a coffee to stay awake while editing ➡ www.buymeacoff...
    🗣️ Find my socials ➡ andfanilo.com
    00:00 Context
    00:22 CSS Pattern
    01:38 Background Image on the net
    02:30 Local background image
    04:14 Resize, reposition, scrollable image
    👉 Links
    - Source code: github.com/and...
    - Streamlit website: streamlit.io/
    - Streamlit docs: docs.streamlit...
    - Streamlit forum: discuss.stream...
    - Streamlit Discord (official): / discord
    My tools (Affiliate links to support me!)
    - 📷 Main Camera - amzn.to/3QvTosB
    - 📷 Main Lens - amzn.to/3svYCwq
    - 🎙️ Microphone - amzn.to/47l5ewA
    - 🎵 Music & Sound Effects - www.epidemicso...
    - 📕 Streamlit Official Book - amzn.to/3QuRPwl
    🪶 26th video out of 35 for the year. Also HEYYY I just broke 1000 subs, thank you everyone for the support! Now let me enjoy a little summer break so I don't stress out about the "9 videos left to do in 4 months" and "4000 hours of watch time" metrics!
    👍 On this channel, we love building a lot of small yet smart Streamlit apps to improve our Python chops, and recall our stories around Data Science and Content Creation. Like & Subscribe if you would like to see more videos!
    #streamlit #python #datascience #dataapps
    ⚠️ Disclaimer: This video is not sponsored. Links included in this description might be affiliate links. If you purchase a product or service with the links that I provide I may receive a small commission. Thank you for supporting my channel so I can continue to provide you with free content!

КОМЕНТАРІ • 89

  • @junealexissantos4341
    @junealexissantos4341 2 роки тому +4

    This is so cool! Hope they add new features that will support these methods natively.

    • @andfanilo
      @andfanilo  2 роки тому +1

      Hey June!
      Wait & See ... maybe enabling CSS parsing for backgroundColor in the config file for theming could already help.
      I think this is the kind of feature that needs activity in this issue (github.com/streamlit/streamlit/issues/2725 ) to be considered

  • @CodingIsFun
    @CodingIsFun 2 роки тому +9

    That is super useful! Thank you!

  • @digitalcivilulydighed
    @digitalcivilulydighed 4 місяці тому

    The best channel for streamlit!

    • @andfanilo
      @andfanilo  4 місяці тому +1

      Thanks for the support 🙏 see you on the next video!

  • @lhcbenac
    @lhcbenac Рік тому +2

    amazing joke " Oh no, not CSS" . Got me laughing for a while on this! Great content keep going , this chanel is really awesome.

    • @andfanilo
      @andfanilo  Рік тому

      Glad you enjoyed it, thanks for the support ! I will keep going and hope to see you around :)

  • @cwenfernandes6072
    @cwenfernandes6072 5 місяців тому

    This is so helpful really solved my problem of adding background to streamlit.. Thank you !!!

    • @andfanilo
      @andfanilo  5 місяців тому

      Glad it helped! Thanks for watching :)

  • @Sonntagssoziologe
    @Sonntagssoziologe Місяць тому

    Oh no css I cried, but now I'm jumping happily through the grass.

  • @rodrigobogado653
    @rodrigobogado653 3 місяці тому

    It's 3 in the morning here and every time I put on streamlit I get fanilo hahahaha. Thanks for the videos bro!

    • @andfanilo
      @andfanilo  3 місяці тому

      😆 Aren't my videos too "exciting" to get you to sleep?! Thanks for the support as always, very appreciated :) also there's a new one coming tomorrow eheh

    • @rodrigobogado653
      @rodrigobogado653 2 місяці тому

      @@andfanilo Very good videos, I was just looking at the backgrounds and how to make something more personalized, thanks man

  • @oleksandrarsentiev7152
    @oleksandrarsentiev7152 Рік тому +1

    exactly what I was looking for! Thanks a lot!

    • @andfanilo
      @andfanilo  Рік тому

      Awesome! What background are you planning to put :D ?

  • @sizeli7120
    @sizeli7120 Рік тому +1

    Fantastic work, very inspiring! Thank you !!

    • @andfanilo
      @andfanilo  Рік тому

      Thanks for your support, much appreciated 😊

  • @alessandroceccarelli6889
    @alessandroceccarelli6889 2 роки тому +2

    Great content as usual 👏🏻👏🏻

    • @andfanilo
      @andfanilo  2 роки тому +1

      Thanks for the endless support! Hope you are enjoying summer :)

  • @flight5056
    @flight5056 2 роки тому +1

    ❣awesome video - super useful.. lots of info packed into 5 min

    • @andfanilo
      @andfanilo  2 роки тому +1

      Thanks for the support! You should be enjoying your summer break though!!

  • @juliocamplaz3212
    @juliocamplaz3212 5 місяців тому

    amazing!!! you just saved me a lot of hours!!! 👏

  • @wooof9470
    @wooof9470 4 місяці тому

    Good video

    • @andfanilo
      @andfanilo  4 місяці тому

      Thanks for watching :)

  • @ananthramkumar9293
    @ananthramkumar9293 10 місяців тому +4

    You need to explain in detail.

    • @andfanilo
      @andfanilo  10 місяців тому

      Hello, you'll have to be more precise on what part you need more detail, otherwise no one can help you ^^"

  • @eugenmalatov5470
    @eugenmalatov5470 6 місяців тому

    Funny presentation and amazing content. A bit too fast for me with all the code in the background (but need to learn to use the k key)
    Anyways, one thing I have not understood.
    So, normally one needs to put the image in the correct static folder. Seems normal and easy.
    You say you want to use a markdown as text (image -> text encoding). Fair enoug, but where do you put the image file?
    I did not get it.

    • @andfanilo
      @andfanilo  5 місяців тому +1

      Hello!
      As per the source code ( github.com/andfanilo/social-media-tutorials/blob/master/20220817-streamlit_css_background/streamlit_app.py ) you can put the image wherever you want, since you are going to read the image in your Python code and encode it as base64 programmatically ( with the `get_img_as_base64("image.jpg")` line, if you put the image in an "assets' folder it will be `get_img_as_base64("assets/image.jpg")`)
      This video being a little old, there is now a better way to do with static file serving ( docs.streamlit.io/library/advanced-features/static-file-serving ).
      1. You enable static file serving through `.streamlit/config.toml` adding:
      ```
      [server]
      enableStaticServing = true
      ```
      2. you put your image in `static` folder at the root of your project, it should now be served at the `localhost:8501/app/static/image.jpg` link
      3. In your st.markdown code, you can point directly to the image url instead of using the base64 representation:
      ```
      import streamlit as st
      page_bg_img = f"""
      [data-testid="stAppViewContainer"] > .main {{
      background-image: url("app/static/image.jpg");
      }}
      """
      st.markdown(page_bg_img, unsafe_allow_html=True)
      ```
      Good luck!

    • @eugenmalatov5470
      @eugenmalatov5470 5 місяців тому

      @@andfanilo Thank you !!!! :))

  • @iputudodiksukmaindranata4599

    That is super useful broo! Thank you!

    • @andfanilo
      @andfanilo  Рік тому

      Happy to help :) thanks for the support!

  • @reflet1501
    @reflet1501 11 місяців тому

    Thanks, this helped out a lot!

    • @andfanilo
      @andfanilo  11 місяців тому

      Glad to hear, looking forward to your app!

  • @darrellardhanihidayat555
    @darrellardhanihidayat555 Рік тому

    hi sensei, first things first thk for the video so i can learn how to customize background on streamlit using css, but i got 1 tiny problem, which is the photo form url is not in the streamlit background, how to fix it sensei🙏🙏

  •  2 роки тому

    Good work. Again !

    • @andfanilo
      @andfanilo  2 роки тому

      Thank you for following and your support :) very appreciated

  • @mahipalbora4679
    @mahipalbora4679 9 місяців тому

    really helpful. How can we change the background color of an ?. I have been really struggling with that

  • @amarnath1828
    @amarnath1828 4 місяці тому

    thank you sir

    • @andfanilo
      @andfanilo  4 місяці тому

      Thanks for watching :) see you on the next one

  • @i.dragons
    @i.dragons 2 роки тому

    Great Work!

    • @andfanilo
      @andfanilo  2 роки тому +1

      Thank you, looking forward to Streamlit with background image apps from you :)

  • @shosh232314
    @shosh232314 Рік тому +1

    Thank u so much🙏🏻

    • @andfanilo
      @andfanilo  Рік тому

      You're welcome 😊 happy Streamlitin'!

  • @user-uo4cy7mx6t
    @user-uo4cy7mx6t 9 місяців тому

    Thank you very much❤
    But when I change background, my app is running slow

    • @andfanilo
      @andfanilo  9 місяців тому +1

      Hey, that's odd..is your image heavy, like a high resolution PNG?

  • @ufk_sn
    @ufk_sn 11 місяців тому

    thanks for the video. I want to change the background color of only one column/container and tried the code below but it changes the color of all of the columns/containers. How can I handle it?
    columns(2)
    with cols [0]:
    page_bg_img = """

    [data-testid="column"] {
    background-color: black;
    }

    """
    st.markdown(page_bg_img,unsafe_allow_html=True)

    • @andfanilo
      @andfanilo  11 місяців тому

      Hey! It's a bit of a pain but discuss.streamlit.io/t/changing-color-of-a-column/10175/3 or ua-cam.com/video/SLyS0v8br20/v-deo.html should help
      I haven't tried but extras.streamlit.app/Styleable%20Container on a specific column css may do the trick too
      Hope this help quickstart something!
      Fanilo

  • @harrygolde6031
    @harrygolde6031 6 місяців тому

    Thank you!

  • @lucasfescina
    @lucasfescina Рік тому

    your videos are awasome

    • @andfanilo
      @andfanilo  Рік тому

      Thanks for the feedback, hope to keep seeing you around!
      What images are you putting in Streamlit background :) ?

    • @lucasfescina
      @lucasfescina Рік тому

      @@andfanilo I gave up when I saw hoe hard it is haha

    • @lucasfescina
      @lucasfescina Рік тому

      Can I ask something: streamlit cloud accept an archive pickle ?

    • @andfanilo
      @andfanilo  Рік тому

      @@lucasfescina From memory anything stored in the Github repo is available in the deployed app. As long as the pickle is not too big and stored in Github it should be fine.
      You can also download it from some remote Google drive location and cache it after deserializing.

  • @tareefroustom6759
    @tareefroustom6759 2 роки тому

    Love it

  • @elenagavrilova3109
    @elenagavrilova3109 9 місяців тому

    thank you!

    • @andfanilo
      @andfanilo  9 місяців тому

      You're welcome! Hope to keep seeing you around :)

  • @DrGauravAI
    @DrGauravAI 2 роки тому

    Awesome

    • @andfanilo
      @andfanilo  2 роки тому

      Looking forward to your masterpiece 🙂

  • @badabd3620
    @badabd3620 Рік тому

    super ta vidéo

    • @andfanilo
      @andfanilo  Рік тому

      Merci pour le soutien 😁

  • @gehhard
    @gehhard 10 місяців тому

    I am Brazilian, is it possible integrate statsmodel with streamlit?

    • @andfanilo
      @andfanilo  10 місяців тому

      Hello,
      Yes, Streamlit is really only providing a web app layer over a Python script, so statsmodel would still work the same
      Have a nice day!

  • @jackychan4640
    @jackychan4640 2 роки тому +1

    Thanks Fanilo Andriannasolo

    • @andfanilo
      @andfanilo  Рік тому +1

      Thanks for watching and for your support :) happy Streamlitin' !

  • @samuelsimons4649
    @samuelsimons4649 2 роки тому

    King!

    • @andfanilo
      @andfanilo  2 роки тому

      Thanks for watching, looking forward to the background image you will choose !

  • @miojovege
    @miojovege Рік тому

    Genius

    • @andfanilo
      @andfanilo  Рік тому

      Thanks for watching, looking forward to seeing your app with a stylish background!

  • @vladigospel
    @vladigospel Рік тому

    tks for the video, I have a problem, when I try to add a local background image with the base64 method, the "No such file or directory: 'image.jpg' " error appears. I have the image in the same folder where I have the .py file, that is correct? I really appreciate your help

    • @andfanilo
      @andfanilo  Рік тому

      Hello Vladimir!
      It will depend on the working directory, which is from where you are running the streamlit command. If you are running from a different folder than where the script is, then the relative path will start from the initial folder, not the script's one. Same issue if you are running a multipage app, the folder for the relative pathing would not be your script but the root main script.
      Don't hesitate to run "st.write(os.getcwd())" to see where you are running this from, and "st.write(os.listdir("./"))" to see if your image.jpg is in this list, and if that's not the case you may need to change "image.jpg" to "../image.jpg" or "folder/image.jpg" to traverse your directory.
      Or not totally tested but the __file__ variable returns the location of the file so using pathlib you could do "(Path(__file__).resolve().parent / "image.jpg"" to go from your script to the script's folder to the image.jpg next to the script.
      Hope this helps
      Fanilo

    • @sahitya2042
      @sahitya2042 Рік тому

      @@andfanilo Hey Fanilo ! I ran " st.write(os.listdir("./")) " and found my image in the list. But still couldn't find my background image in my website. Could You please help ?
      Thank You
      Sahitya

    • @haripriyareddy7978
      @haripriyareddy7978 7 місяців тому

      ​@@sahitya2042did u get it if so kindly help me to make it done even I am facing the same issue

  • @asifkhanpattan9982
    @asifkhanpattan9982 8 місяців тому

    can i know from where u had learn this??

    • @andfanilo
      @andfanilo  8 місяців тому

      Hey! Hmmm, from experience in building HTML/CSS frontends and a bit of forum reading discuss.streamlit.io/

  • @RandyGriggs
    @RandyGriggs 11 місяців тому

    Hey Fanilo, This is not working for me when I test on my local machine. Can you advise? @st.cache_data
    def get_img_as_base64(file):
    with open(file, "rb") as f:
    data = f.read()
    return base64.b64encode(data).decode()
    img = get_img_as_base64("./images/DeltaPacific_Logo.jpg")
    [data-testid="stSidebar"] > div:first-child {{
    background-image: url("data:image/jpg;base64,{img}");
    background-size: 200px;
    background-repeat: no-repeat;
    background-position: center;
    }}
    Thank you in advance

  • @ainanirina758
    @ainanirina758 Рік тому

    Tsy nampoiziko ity chaine ity hoe gasy, mafinaritra!
    Aty Madagascar ve enao?

  • @estelari
    @estelari Рік тому

    thankssss

    • @andfanilo
      @andfanilo  Рік тому

      Your welcome =) what background are you trying to put?

  • @ArminEskandary
    @ArminEskandary 2 роки тому

    Hi bro . I need ur help . I want to prevent my app from screen capture and screenshots . Is there any html/css markdown code for it ??

    • @andfanilo
      @andfanilo  2 роки тому

      Hey! No I don't think so, screen capture is system level so no app could have access to those to prevent it :/ what are you trying to achieve?

    • @ArminEskandary
      @ArminEskandary 2 роки тому

      @@andfanilo yeah . There are some websites that prevents recording such as spot player and netflix-us

    • @andfanilo
      @andfanilo  2 роки тому

      I mean, I could still capture the live video/audio feed with OBS for example, so even if you can prevent it at browser-level, I can capture the live feed.
      But I think I understand, there are ways to make it harder from stackoverflow.com/questions/9756837/prevent-html5-video-from-being-downloaded-right-click-saved . Apparently by adding 'oncontextmenu="return false;"' to any video element it would be doable. Using the technique described here (ua-cam.com/video/SLyS0v8br20/v-deo.html) to leave the component , target all video elements and apply that contextmenu, yeah it could work.
      I haven't tried since I'm actually on a summer break :) but let me know if that helps a bit

  •  Рік тому

    haha I like your voice, so impressive and attractive

    • @andfanilo
      @andfanilo  Рік тому

      Thank you for the compliment, have a nice day!

  • @gauthamsuresh4316
    @gauthamsuresh4316 3 місяці тому

    Bro when you are explaining , please say it completely and clearly please. Most of people in streamlit community wants colour-gradient i their code , and you just said do this ....etc. Everything is in a big hurry. Please bro , youtube time limit is 10 or 12 mins right? So please , say things slowly and clearly . So that any beginner who watches your video can learn.
    Clearly I am confused. Your gradient method isnt working in my code. I wrote the complete argument too. See? this is what i meant by not understanding bro. I'm not hating on you. But people came to see your video , watching yo thumbnail about some basic css styling and you explained everything in a jiffy.

  • @fabienbenoit6650
    @fabienbenoit6650 6 місяців тому

    Dude you miss the whole point while trying so hard to be funny the worst presentation ever

    • @andfanilo
      @andfanilo  6 місяців тому

      Too bad 🙂 have a nice day!

  • @flynneugene2675
    @flynneugene2675 Місяць тому

    bad