Search code examples
pythonfile-uploadshiny-reactivitypy-shiny

Storage location for files uploaded to Python Shiny Express application?


I'm trying to allow users to upload a file to a web app that I've built using Shiny Express for Python. I'm using the ui.input_file function for users to select the file that they'd like to upload, but the documentation doesn't mention a location where uploaded files will be stored beyond a datapath. When you try to specify the datapath like so:

from shiny import reactive
from shiny.express import input, render, ui
from shiny.types import FileInfo

ui.input_file("file1", "please select a file to upload", accept=['.csv'], multiple = False,)
                
@reactive.calc
def parsed_file():
    file: list[FileInfo] | None = input.file1()
    f = open(file[0]["datapath"], "r")
    print(f)
    return f

Nothing is returned and nothing is printed. When I change datapath to Path(__file__).parent / "./data/results/", the problem doesn't get fixed and nothing is stored in the location I've specified. I've also tried to look at my git logs to see if there are any changes when I try to upload a file, but there are no changes.

Could the file be going to my /tmp files? If so, how do I change where the file is put?


Solution

  • Try this:

    from shiny import reactive
    from shiny.express import input, render, ui
    from shiny.types import FileInfo
    import pandas as pd
    
    ui.input_file("file1", "please select a file to upload", accept=['.csv'], multiple = False,)
    
    @reactive.calc
    def parsed_file():
        if not input.file1():
            return
        df = pd.read_csv(input.file1()[0]["datapath"])
    
        return df
    
    @render.table
    def summary():
        df = parsed_file()
        return df