Inside a class, how to watch for changes in a global variable?

maybe this

import panel as pn
import param
import pandas as pd 
import io

pn.extension()

file_upload = pn.widgets.FileInput(accept=".xlsx")

class DataSelector(param.Parameterized):

    select = param.Selector(["Please upload data"])
    
    def __init__(self, file_upload, **params):
        super().__init__(**params)
        self.file_upload = file_upload
        
    def file_callback(self, event):
        if event.new:
            data = io.BytesIO()
            self.file_upload.save(data)
            data.seek(0)
            df = pd.read_excel(data)
            self.param.select.objects = sorted(df.columns.to_list(), key=str.lower)
            
# Pass in file_upload   
data_selector = DataSelector(file_upload=file_upload) 

file_upload.param.watch(data_selector.file_callback, 'value')

pn.Column(
    file_upload, 
    data_selector.param.select
).servable()
2 Likes