Pause Appmode to wait for user input

I am currently using Appmode to create a web application out of of my notebook. The notebook is quite simple, it loads a data frame (Excel sheet) and creates some interactive plots. I would like to give the ‘user’ the possibility to choose the file (Excel sheet) to load while being in Appmode, so without having to change the code and rerun again the Appmode. This is very easy to do in the notebook, for instance:

uploader = FileUpload()
file_name = list(uploader.value.values())[0]['metadata']['name']
df = pd.read_excel(file_name)

However, this does not work in Appmode, since all the cells of the notebook are run before giving the user the opportunity to select the file:

IndexError                                Traceback (most recent call last)
<ipython-input-4-b5e06bffc3b9> in <module>
----> 1 file_name = list(uploader.value.values())[0]['metadata']['name']
      2 df = pd.read_excel(file_name)

IndexError: list index out of range

Is there a way to implement this? I was looking into possible Asynchronous Widgets, but I cannot make them work.

Thanks in advance for your help.

See this discussion here.
I actually was curious if what I did to allow the user to choose what data to use (I provide a default file they can choose for a demo if no other data uploaded) before making a plot with widgets controllers in Voila using ipywidgets works in appmode? I thought the same code might work since both Voila and appmode use widgets, and it seems it does!
I found when I launched from the appmode repo and used curl to get that notebook and the accompanying data, it worked in appmode once I installed the additional dependencies (namely numpy, seaborn, pandas) & used the classic notebook interface.

By the way, GitHub - guiwitz/voila_image_processing: Example of how to use voila to serve image processing solutions , see here, was the basis for image selection and file upload code that I adapted.

1 Like

Thanks a lot for your reply Wayne, it looks like the solution I am looking for! But does this mean that I should wrap my entire notebook inside a function? And that anyway the notebook needs to be run at least once in Appmode with a default file, before accepting a new one?

I don’t think it needs to be in a single function or cell; however, it needs to be set up to work together as an app. If you look at the calculator code example for appmode, along with what I’ve linked, it should guide you.

No, I think if you run my example without getting the demo .tsv file, it will wait until you upload a .tsv file and select it to process it. It doesn’t have to have run first with any data.
It’s just that I provide a file to show up in file selection listing when people launch my example repo via MyBinder. That way they don’t need to actually upload anything to run the code and see what would happen with their own data.