Knowing that Streamlit uses watchdog to monitor source codes, it seams a very appealing feature to be able to watch the user files. For example reloading the app when a data file changes.
Problem
The code in the link was exactly designed for this use case, but it’s no longer working because of changes in Streamlit source code in the newer versions !
After some hacking, I managed to get the mentioned code working. Here is my version:
from datetime import datetime as dt
import pandas as pd
import streamlit as st
import gc
import os
from streamlit.web.server import Server
from streamlit.runtime.scriptrunner import get_script_run_ctx
# get run context
ctx = get_script_run_ctx()
# get session id
session_id = ctx.session_id
# get server
for obj in gc.get_objects():
if type(obj) is Server:
server = obj
break
# get session
session_info = server._runtime._get_session_info(session_id)
session = session_info.session
# register watcher
session._local_sources_watcher._register_watcher(
os.path.join(os.path.dirname(__file__), 'watched.csv'),
'dummy:watched.csv'
)
st.text(dt.now())
st.dataframe(pd.read_csv('watched.csv', sep=';', decimal=','))
This mostly works fine, except when I try to modify watched.csv with excel while the App is running, this raise this exception :
Exception in thread Thread-1:
Traceback (most recent call last):
File "C:\Users\user_name\bin\miniconda3-4.9.2\lib\threading.py", line 926, in _bootstrap_inner
self.run()
File "C:\Users\user_name\bin\miniconda3-4.9.2\lib\site-packages\watchdog\observers\api.py", line 205, in run
self.dispatch_events(self.event_queue)
File "C:\Users\user_name\bin\miniconda3-4.9.2\lib\site-packages\watchdog\observers\api.py", line 381, in dispatch_events
handler.dispatch(event)
File "C:\Users\user_name\bin\miniconda3-4.9.2\lib\site-packages\watchdog\events.py", line 278, in dispatch
}[event.event_type](event)
File "C:\Users\user_name\bin\miniconda3-4.9.2\lib\site-packages\streamlit\watcher\event_based_path_watcher.py", line 366, in on_created
self.handle_path_change_event(event)
File "C:\Users\user_name\bin\miniconda3-4.9.2\lib\site-packages\streamlit\watcher\event_based_path_watcher.py", line 355, in handle_path_change_event
allow_nonexistent=changed_path_info.allow_nonexistent,
File "C:\Users\user_name\bin\miniconda3-4.9.2\lib\site-packages\streamlit\watcher\util.py", line 56, in calc_md5_with_blocking_retries
content = _get_file_content_with_blocking_retries(path)
File "C:\Users\user_name\bin\miniconda3-4.9.2\lib\site-packages\streamlit\watcher\util.py", line 92, in _get_file_content_with_blocking_retries
content = f.read()
PermissionError: [Errno 13] Permission denied
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.