当前位置:   article > 正文

streamlit设置sidebbar和页面背景_streamlit设置背景图片

streamlit设置背景图片
streamlit==1.29.0

1. 设置sidebar背景

代码

  1. import base64
  2. import streamlit as st
  3. def sidebar_bg(side_bg):
  4. side_bg_ext = 'png'
  5. st.markdown(
  6. f"""
  7. <style>
  8. [data-testid="stSidebar"] > div:first-child {{
  9. background: url(data:image/{side_bg_ext};base64,{base64.b64encode(open(side_bg, "rb").read()).decode()});
  10. }}
  11. </style>
  12. """,
  13. unsafe_allow_html=True,
  14. )
  15. #调用
  16. sidebar_bg('./pics/background1.jpg')

效果

2. 设置页面背景

代码:

  1. def main_bg(main_bg):
  2. main_bg_ext = "png"
  3. st.markdown(
  4. f"""
  5. <style>
  6. .stApp {{
  7. background: url(data:image/{main_bg_ext};base64,{base64.b64encode(open(main_bg, "rb").read()).decode()});
  8. background-size: cover
  9. }}
  10. </style>
  11. """,
  12. unsafe_allow_html=True
  13. )
  14. #调用
  15. main_bg('./pics/background.jpg')

效果: 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家小花儿/article/detail/175105
推荐阅读
  

闽ICP备14008679号