Skip to content

FileInput

Bases: InputElement

_value_type: type property

Get the FileInput value type: either a list of string list[str] when the FileInput is multiple file selection, otherwise a single string str.

value: Optional[Union[List[str], str]] property

Returns:

Type Description
Optional[Union[List[str], str]]

The path or list of paths for the selected file(s): if paths are not absolute, then

Optional[Union[List[str], str]]

they are considered relative to the data root folder. See

Optional[Union[List[str], str]]

Best Practices With Data for more information.

__init__(key, value, label=None, count=None, optional=False, hide_when_disabled=False, types=None, multiple=False, tags=None)

A single or multiple file selector.

Parameters:

Name Type Description Default
key str

ID of the element. It must be unique as it is the key used to story data in Project(), otherwise it will lead to conflicts at runtime in both execution and Streamlit modes. The key will be transformed into snake case and slugified to avoid any special character or whitespace. Note that an ID cannot start with _. Try to choose a key that is meaningful for your context (see examples projects).

required
value Optional[Union[str, List[str], List[List[str]]]]

Path to file(s). Provided file(s) don't necessarily have to exist for the Streamlit mode, however their existence will be checked at execution time. If paths are not absolute, then they are considered relative to the data root folder. See Best Practices With Data for more information.

required
label Optional[str]

Label to display left of the file selector.

None
count Optional[Union[int, str]]

Specify the number of occurence of the widget. OneCode typically uses it for the streamlit case. Note that if count is defined, the expected value should always be a list, even if the count is 1. count can either be a fixed number (e.g. 3) or an expression dependent of other elements (see Using Expressions for more information).

None
optional Union[bool, str]

Specify whether the value may be None. optional can either be a fixed boolean (False or True) or a conditional expression dependent of other elements (see Using Expressions for more information).

False
hide_when_disabled bool

If element is optional, set it to True to hide it from the interface, otherwise it will be shown disabled.

False
types List[Tuple[str, str]]

List of filters allowing to narrow file selection within Streamlit. Each filter must be a pair of (name, list of allowed extensions), e.g. ("Image", ".jpg .png .jpeg"). You may use the FileFilter enums for convenience.

None
multiple bool

Set to True if multiple choice is allowed, otherwise only a single element can be selected.

False
tags Optional[List[str]]

Optional meta-data information about the expected file. This information is only used by the Mode.EXTRACT_ALL when dumping attributes to JSON.

None

Raises:

Type Description
ValueError

if the key is empty or starts with _.

AttributeError

if one the kwargs conflicts with an existing attribute or method.

Example

from onecode import file_input, Mode, Project

Project().mode = Mode.EXECUTE
widget = file_input(
    key="FileInput",
    value=["/path/to/file1.txt", "/path/to/file2.csv"],
    multiple=True,
    tags=['MyTags']
)
print(widget)
Output
["/path/to/file1.txt", "/path/to/file2.csv"]

_validate(value)

Validate the selected value (see _validate_option_value()). In case of multipe file selection, each choice is validated individually.

_validate_file_value(value)

Raises:

Type Description
FileNotFoundError

if the path does not exist or is not a file.

streamlit(id)

Returns:

Type Description
str

The Streamlit code for a file selection (st.text_input for the path combined with a

str

tkinter.filedialog.askopenfilename for the file selection).