Skip to content

Instantly share code, notes, and snippets.

@poojatambe
Created June 6, 2022 16:17
Show Gist options
  • Save poojatambe/fa6a763116baaef4b0bfa63e292cfb41 to your computer and use it in GitHub Desktop.
Save poojatambe/fa6a763116baaef4b0bfa63e292cfb41 to your computer and use it in GitHub Desktop.
# background image to streamlit
@st.cache(allow_output_mutation=True)
def get_base64_of_bin_file(bin_file):
with open(bin_file, 'rb') as f:
data = f.read()
return base64.b64encode(data).decode()
def set_png_as_page_bg(png_file):
bin_str = get_base64_of_bin_file(png_file)
page_bg_img = '''
<style>
.stApp {
background-image: url("data:image/png;base64,%s");
background-size: cover;
background-repeat: no-repeat;
background-attachment: scroll; # doesn't work
}
</style>
''' % bin_str
st.markdown(page_bg_img, unsafe_allow_html=True)
return
set_png_as_page_bg('/content/background.webp')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment