Help on live updates on files and folders


I would like to ask help on live updates. I read this topic and copied some of the codes and concept:

Dash app not reading modified file on refreshing the browser

But mine is a bit different. This would be an updates on folders inside X:/rootfolder instead of a CSV file.

sample code:

import dash
from dash import dcc, html, callback, Output, Input, Dash, no_update
from dash_table import DataTable
import dash_bootstrap_components as dbc
import plotly.graph_objs as go
import pandas as pd
import os
import win32api
from datetime import date
from plotly.subplots import make_subplots

path = ‘X:/rootfolder’
lastmt = os.stat(path).st_mtime

####files and folders####
path_models_splited1_filtered =
path_models =
drives_aoi = None
building = None
building_name = None

def get_disk(request_type):

def get_drive_names():
    drives = win32api.GetLogicalDriveStrings()

    drives = drives.split('\000')[:-1]

    drive_names = []
    for drive in drives:
            drive_name = win32api.GetVolumeInformation(drive)[0]
            drive_names.append((drive, drive_name))

        except Exception as e:
            drive_names.append((drive, "Unknown"))

    return drive_names

names = []
driveletters = []
drive_list = get_drive_names()

for drive, name in drive_list:
    if not drive == "C:\\":
        if not drive == "D:\\":

striped_backslash = [s.strip('\\') for s in driveletters]

replaced_w_slash = []
for striped in striped_backslash:
    replaced_w_slash.append(striped + '/')

drives_aoi_folder = []
for drives_aoi in replaced_w_slash:
    drives_aoi_folder.append(drives_aoi + 'AOI')

match request_type:
    case "drive_letter":
        return replaced_w_slash
    # match request_type:
    case "drive_label":
        return names
    case "drives_aoi_folder":
        return drives_aoi_folder

def get_folders(last_path):

   value = os.listdir(last_path)
   return value

def handle_filesystem_models():

building = get_disk("drive_letter")
drives_aoi = get_disk("drives_aoi_folder")
building_name = get_disk("drive_label")

models = []
for driveaoi in drives_aoi:
#models path
# path_models = []
path_models_splited1 = []
for model, drive in zip(models, drives_aoi):
    for model_i in model:
        path_models.append(drive + '/' + model_i)
for pth_mdl in path_models:
##filter duplicate folder names
path_models_splited1_filtered = list(dict.fromkeys(path_models_splited1))
return path_models_splited1_filtered

path_models_splited1_filtered = handle_filesystem_models()
####files and folders####

app = Dash(name)
app.layout = html.Div(
dcc.Dropdown(options=[{‘label’: value, ‘value’: value} for value in path_models_splited1_filtered], id=‘dropdown’,


    dcc.Interval(id='interval', interval=1000, n_intervals=0)


@app.callback(Output(‘dropdown’, ‘value’), [Input(‘interval’, ‘n_intervals’)])
def trigger_by_modify(n):

  global lastmt
  global names
   global path_models_splited1_filteredin
   if os.stat(path).st_mtime > lastmt:
       lastmt = os.stat(path).st_mtime
       path_models_splited1_filteredin = handle_filesystem_models()
       print(path_models_splited1_filteredin )

       return path_models_splited1_filteredin 

   return no_update

if name == “main”:
app.run_server(debug=True, port=8051)

folders under X:/rootfolder, I will make it as a dropdown menu and append it in list path_models_splited1_filtered = [ ‘subfolder1’, ‘subfolder2’ ]

My goals is if I create subfolder3 inside X:/rootfolder, I’m going to see it in dropdown menu. However, that is not the case. path_models_splited1_filteredin the trigger_by_modify() will be updated if I create new folder (printed it). How can I pass the new value of path_models_splited1_filtered into the dropdown menu?

anyone can help is much appreciated. Let me know if you need further clarifications :smiley: