How to extend streamlit watchers to monitor custom user files?

Continuing the discussion from How to monitor the filesystem and have streamlit updated when some files are modified?:


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.


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
# get session
session_info = server._runtime._get_session_info(session_id)
session = session_info.session
# register watcher
    os.path.join(os.path.dirname(__file__), 'watched.csv'),

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\", line 926, in _bootstrap_inner
  File "C:\Users\user_name\bin\miniconda3-4.9.2\lib\site-packages\watchdog\observers\", line 205, in run
  File "C:\Users\user_name\bin\miniconda3-4.9.2\lib\site-packages\watchdog\observers\", line 381, in dispatch_events
  File "C:\Users\user_name\bin\miniconda3-4.9.2\lib\site-packages\watchdog\", line 278, in dispatch
  File "C:\Users\user_name\bin\miniconda3-4.9.2\lib\site-packages\streamlit\watcher\", line 366, in on_created
  File "C:\Users\user_name\bin\miniconda3-4.9.2\lib\site-packages\streamlit\watcher\", line 355, in handle_path_change_event
  File "C:\Users\user_name\bin\miniconda3-4.9.2\lib\site-packages\streamlit\watcher\", 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\", line 92, in _get_file_content_with_blocking_retries
    content =
PermissionError: [Errno 13] Permission denied
1 Like