Hi,
I want to use custom font using woff font but it does not work. I suppose it is access to local font url.
Can you tell me what is wrong in my code?
Thanks.
Hi Caroline,
My problem is that my app is on a local server with no access to internet, so no Github repo.
I suppose there is a mistake in the way I reference the url but I don’t see what.
Thanks.
David
@dnaura This is not recommended for anything but a locally running streamlit app with no internet access, but there’s a hack you can do that moves the font file to wherever streamlit is installed on your machine, and then references that. CSS won't render in deployed app (using components.html())
If you have internet access, it’s much better to reference a file somewhere on the internet, but in your case this should work.
Here’s a working script:
import shutil
from pathlib import Path
import streamlit as st
def move_font_files():
STREAMLIT_STATIC_PATH = Path(st.__path__[0]) / "static"
CSS_PATH = STREAMLIT_STATIC_PATH / "assets/fonts/"
if not CSS_PATH.is_dir():
CSS_PATH.mkdir()
css_file = CSS_PATH / "FuturaCyrillicLight.woff"
if not css_file.exists():
shutil.copy("layout/FuturaCyrillicLight.woff", css_file)
st.markdown(
"""
<style>
@font-face {
font-family: 'Futura PT Light';
font-style: normal;
font-weight: normal;
src: local('Futura PT Light'), url('assets/fonts/FuturaCyrillicLight.woff') format('woff');
}
html, body, [class*="css"] {
font-family: 'Futura PT Light', sans-serif;
}
</style>
""",
unsafe_allow_html=True,
)
st.write("Test!")
if st.button("Move font files"):
move_font_files()
Thanks for stopping by! We use cookies to help us understand how you interact with our website.
By clicking “Accept all”, you consent to our use of cookies. For more information, please see our privacy policy.
Cookie settings
Strictly necessary cookies
These cookies are necessary for the website to function and cannot be switched off. They are usually only set in response to actions made by you which amount to a request for services, such as setting your privacy preferences, logging in or filling in forms.
Performance cookies
These cookies allow us to count visits and traffic sources so we can measure and improve the performance of our site. They help us understand how visitors move around the site and which pages are most frequently visited.
Functional cookies
These cookies are used to record your choices and settings, maintain your preferences over time and recognize you when you return to our website. These cookies help us to personalize our content for you and remember your preferences.
Targeting cookies
These cookies may be deployed to our site by our advertising partners to build a profile of your interest and provide you with content that is relevant to you, including showing you relevant ads on other websites.