Fixing SEO for Streamlit Apps!
In this short article, I describe how I added meta tags to my Consultancy Streamlit App in a way that won't break if there is a Streamlit update ✨
I am building my Career Consultancy website using the Streamlit framework, a popular framework that allows you to create an interactive web app in pure Python.
🕷 The Problem
As I got closer to release my website, I deployed the containerized web app to a staging instance in the Azure Cloud, I realized that every time I shared a link to my website, it would look something like this:
🔎 Why does it happen?
Streamlit does not support adding meta tags natively to a custom deployment!
This is necessary so that the title, picture and description of the website appears when shared via Social Media or messaging apps like WhatsApp.
👨🏻🔬 The Research
After searching online, and thanks to Salman Chen for writing this article, it became evident that the best way of doing this was manually copying the index.html which comes inside the streamlit library, adding the meta tags, and overwriting the original one.
The article gave me an idea of how to solve the problem in this particular deployment, but the workaround had an issue:
What happens when I update the Streamlit library in a future deployment?
❌ Probably it will break if the structure of the index.html that the library expects is different than the one we overwrite it with.
😎 The Solution
To future-proof Salman Chen´s approach, and using the help of the Cursor IDE, I came up with a solution that programmatically would:
Locate the index.html file in the Streamlit package directory.
Create a backup of the original index.html file.
Read the content of the original index.html.
Use BeautifulSoup to parse the HTML content.
Create new meta tags with the specified content.
Add these new meta tags to the <head> section of the HTML.
Save the modified HTML, overwriting the original index.html.
✨These steps are achieved by the following script:
🏃🏻♂️How do I run my Script?
Since my application is containerized, I use a Dockerfile. In the Dockerfile, I have written instructions to copy and run the script to insert the meta tags.
# Use the official lightweight Python image. | |
# https://hub.docker.com/_/python | |
FROM python:3.9-slim | |
# Set the working directory | |
WORKDIR /app | |
# Copy the requirements file into the container | |
COPY requirements.txt . | |
# Install the dependencies | |
RUN pip install --no-cache-dir -r requirements.txt | |
# Copy the add_meta_tags.py script | |
COPY add_meta_tags.py . | |
# Run the script to add meta tags | |
RUN python add_meta_tags.py | |
# Copy the rest of the application code into the container | |
COPY . . | |
# Expose the port Streamlit runs on | |
EXPOSE 8501 | |
# Run the application | |
CMD ["streamlit", "run", "src/streamlit_app.py", "--server.port=8501", "--server.address=0.0.0.0"] |
✅ The Results
Now, sharing the my website via Social Media or WhatsApp looks much better! 🎉
🤓 Also, doing this programmatically ensures that even if Streamlit is updated, it won’t break. since it always works on the basis of respecting Streamlit´s original index.html structure!
Note: the script will only break if Streamlit team decides to place their index.html in a different directory, since I am using this line to locate it:
# Path to the Streamlit package directory
streamlit_path = '/usr/local/lib/python3.9/site-packages/streamlit/static/index.html'
Thank you so much!