shiny.ui.input_file#

shiny.ui.input_file(id, label, *, multiple=False, accept=None, width=None, button_label='Browse...', placeholder='No file selected', capture=None)#

Create a file upload control that can be used to upload one or more files.

Parameters
  • id (str) – An input id.

  • label (Union[Tagifiable, Tag, MetadataNode, str, TagList, float, None, Dict[str, Union[str, float, bool, None]], Sequence[Union[Tagifiable, Tag, MetadataNode, str, TagList, float, None, Dict[str, Union[str, float, bool, None]], Sequence[ForwardRef]]]]) – An input label.

  • multiple (bool) – Whether the user should be allowed to select and upload multiple files at once.

  • accept (Union[str, List[str], None]) –

    Unique file type specifier(s) which give the browser a hint as to the type of file the server expects. Many browsers use this to prevent the user from selecting an invalid file. Examples of valid values include:

    • A case insensitive extension like .csv or .rds.

    • A valid MIME type, like text/plain or application/pdf

    • One of audio/*, video/*, or image/* meaning any audio, video, or image type, respectively.

  • width (Optional[str]) – The CSS width, e.g. ‘400px’, or ‘100%’

  • button_label (str) – The label used on the button.

  • placeholder (str) – The text to show on the input before a file has been uploaded.

  • capture (Optional[Literal[‘environment’, ‘user’]]) – On mobile devices, this can be used to open the device’s camera for input. If “environment”, it will open the rear-facing camera. If “user”, it will open the front-facing camera. By default, it will accept either still photos or video. To accept only still photos, use accept="image/*"; to accept only video, use accept="video/*".

Return type

A UI element.

Notes

Server value

A list of dictionaries (one for each file upload) with the following keys:

  • name: The filename provided by the web browser. This is not the path to read to get at the actual data that was uploaded (see ‘datapath’).

  • size: The size of the uploaded data, in bytes.

  • type: The MIME type reported by the browser (for example, ‘text/plain’), or empty string if the browser didn’t know.

  • datapath: The path to a temp file that contains the data that was uploaded. This file may be deleted if the user performs another upload operation.

See also

download_button

Example

#| standalone: true
#| components: [editor, viewer]
#| layout: vertical
#| viewerHeight: 400
from shiny import *
from shiny.types import FileInfo
import pandas as pd

app_ui = ui.page_fluid(
    ui.layout_sidebar(
        ui.panel_sidebar(
            ui.input_file("file1", "Choose CSV File", accept=[".csv"], multiple=False),
            ui.input_checkbox("header", "Header", True),
        ),
        ui.panel_main(ui.output_ui("contents")),
    )
)

def server(input: Inputs, output: Outputs, session: Session):
    @output
    @render.ui
    def contents():
        if input.file1() is None:
            return "Please upload a csv file"
        f: list[FileInfo] = input.file1()
        df = pd.read_csv(f[0]["datapath"], header=0 if input.header() else None)
        return ui.HTML(df.to_html(classes="table table-striped"))


app = App(app_ui, server)