How to monitor the filesystem and have streamlit updated when some files are modified?

I saw that it was possible to use the PYTHONPATH environmental variable to set the python files to monitor. The thing is that you need to import the function in the main function for this to work, you can’t import the module and then call module.function (see my post here: Watching custom folders - #5 by Gabriel_Lema).
So I’m currently using this workaround:

import inspect                                                                                                                                                                                                     
import os 
import streamlit as st                                                                                                                                                                                             
from streamlit import session_state as st_session                                                                                                                                                                  
from mypackage.module import foo, bar                                                                                                                                                                                
                                                                                                                                                                                                                 
def monitor_func(*args):                                                                                                                                                                                                              
    if 'monitor' not in st_session:                                                                                                                                                                                                   
        python_path = os.environ.get('PYTHONPATH')                                                                                                                                                                                    
        if python_path is None:                                                                                                                                                                                                       
            st_session['monitor'] = set()                                                                                                                                                                                             
        else:                                                                                                                                                                                                                         
            st_session['monitor'] = set(python_path.split(':'))                                                                                                                                                                                            
    for func in args:                                                                                                                                                                                                                 
        path = inspect.getsourcefile(func)                                                                                                                                                                                            
        st_session['monitor'].add(os.path.dirname(path))                                                                                                                                                                              
    os.environ['PYTHONPATH'] = ':'.join(list(st_session['monitor'])) 
2 Likes