How to refer to data uploaded with FileInput for further processing?

There are a lot of ways to do it (I’d recommend using param to create a parameterized object), but one easy solution is to just make df a global variable:

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

def load_excel(data):
    if data is not None:
        global df
        df = pd.read_excel(io.BytesIO(data))
        return df

active_load_excel = pn.bind(load_excel, file_input.param.value)

print(active_load_excel())

pn.Column(file_input, active_load_excel)

Here is a (sloppy) example with param:

import panel as pn
import param
import pandas as pd
import io
pn.extension()

class Data(param.Parameterized):
    df = param.DataFrame()
    
    def loader(self):
        file_input = pn.widgets.FileInput(accept=".xlsx")
        
        @pn.depends(file_input, watch = True)
        def load_excel(finput=file_input, self=self):
            data = file_input.value
            if data is not None:
                self.df = pd.read_excel(io.BytesIO(data))
                print (self.df)
                
        return file_input

data = Data()

pn.Column(data.loader, pn.widgets.DataFrame.from_param(data.param['df'], height = 500))
2 Likes