I am trying to build my dashboard application, and I want to build a header component consisting of a background image and other CSS styles or fonts. I used the ad-hoc() st.markdown() but the CSS file could not render. So I turned to the standard streamlit components. It did not work as well.
Here is the script
import streamlit as st
import streamlit.components.v1 as components
from PIL import Image
def main():
#Set page config
st.set_page_config(
page_title="Global Food Price Tracker and Explorer",
layout="wide",
initial_sidebar_state="expanded",
page_icon=Image.open("assets/fruitbasket.png")
)
#render html components
components.html(
html="""
<link rel="stylesheet" href="assets/mystyles.css">
<link href="https://fonts.googleapis.com/css2?family=Roboto+Condensed rel="stylesheet">
<div class="header">
<H1 class="title">Global Food Price Tracker and Explorer</H1>
</div>
"""
)
if __name__ == '__main__':
main()
The image below is the output. As you can see, there is no background image or change in font, except for the text.
At first, I thought I was at fault, so I checked. I pasted the assets folder on the desktop and created a test.html file to run the offline HTML script in my browser. Remarkably, it worked.
So what is wrong? I don’t know. So I changed strategy and decided to internalize the CSS script as you can see:
import streamlit as st
import streamlit.components.v1 as components
from PIL import Image
#Set page config
st.set_page_config(
page_title="Global Food Price Tracker and Explorer",
layout="wide",
initial_sidebar_state="expanded",
page_icon=Image.open("assets/fruitbasket.png")
)
#render html components
components.html(
html="""
<style>
.header {
position: relative;
text-align: center;
color: white;
height: 300px;
background-image: url("assets/wheatfarm.png");
background-position: center;
.title {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
font-family: 'Roboto Condensed', sans-serif;
font-size: 36px;
font-weight: bold;
}
</style>
<link href="https://fonts.googleapis.com/css2?family=Roboto+Condensed rel="stylesheet">
<div class="header">
<H1 class="title">Global Food Price Tracker and Explorer</H1>
</div>
"""
)
This was the strange output:
Can anyone interpret this? Why did the CSS script run well offline but could not run on the Streamlit server? I even put the CSS file to see if it would work but it did not. Someone should please help me out. I am about to quit and move the entire project to Plotly’s Dash.
Please note that I am doing this project in a non-base conda environment. And I am using Opera browser, JupyterLab IDE (notebook + console +
editor).