Hi guys,
I was trying to figure out some ways to add google analytics, and search console meta tags to my streamlit app for Google search indexing, and also to add some additional meta descriptions about my app for better Seo ranking. I added the following code snippet (which I got from this post) to test out the waters before adding the actual search console mate tags:
import streamlit as st
import os
def replace_in_file(filename, oldvalue, newvalue):
"""Replace string in a file and optionally create backup_filename."""
# Read in the file
with open(filename, "r") as f:
filedata = f.read()
# Replace the target string
filedata = filedata.replace(oldvalue, newvalue)
# Write the file out again
with open(filename, "wb") as f:
f.write(filedata)
st_dir = os.path.dirname(st.__file__)
index_filename = os.path.join(st_dir, "static", "index.html")
metadata = """<meta name="author" content="My Name, My Email">"""
replace_in_file(index_filename, "<head>", "<head>" + metadata)
print("Inserted metadata into:", index_filename)
Using this code snippet worked just fine locally, but is causing issues on the streamlit community cloud, I’m getting the following error:
PermissionError: This app has encountered an error. The original error message is redacted to prevent data leaks. Full error details have been recorded in the logs (if you're on Streamlit Cloud, click on 'Manage app' in the lower right of your app).
Traceback:
File "/home/adminuser/venv/lib/python3.11/site-packages/streamlit/runtime/scriptrunner/script_runner.py", line 589, in _run_script
exec(code, module.__dict__)
File "/mount/src/my-resume/Home.py", line 328, in <module>
main()
File "/mount/src/my-resume/Home.py", line 33, in main
replace_in_file(index_filename, "<head>", "<head>" + tracking_code)
File "/mount/src/my-resume/Home.py", line 323, in replace_in_file
with open(filename, "wb") as f:
^^^^^^^^^^^^^^^^^^^^
Looking into the logs, I can see the following errors:
PermissionError: [Errno 13] Permission denied:
'/home/adminuser/venv/lib/python3.11/site-packages/streamlit/static/index.html'
2024-06-30 08:48:25.291 503 GET /script-health-check (127.0.0.1) 195.73ms
────────────────────── Traceback (most recent call last) ───────────────────────
/home/adminuser/venv/lib/python3.11/site-packages/streamlit/runtime/scriptru nner/script_runner.py:589 in _run_script/mount/src/my-resume/Home.py:328 in <module>
325
326
327 if __name__ == "__main__":
❱ 328 │ main()
329
330
/mount/src/my-resume/Home.py:33 in main
30 index_filename = os.path.join(st_dir, "static", "index.html")
31
32 metadata = """<meta name="author" content="My Name
❱ 33 replace_in_file(index_filename, "<head>", "<head>" + metadata)
34 print("Inserted tracking code into:", index_filename)
35
/mount/src/my-resume/Home.py:323 in replace_in_file
320 │ filedata = filedata.replace(oldvalue, newvalue)
321 │
322 │ # Write the file out again
❱ 323 │ with open(filename, "wb") as f:
324 │ │ f.write(filedata)
325
326
I tried changing the write permission from wb
to w+
, but the error persisted.
streamlit==1.35.0, Python==3.10.13