Skip to content

CsvReader

Bases: InputElement

_value_type: type property

Get the CsvReader value type: Pandas DataFrame pd.DataFrame.

value: Optional[pd.DataFrame] property

Returns:

Type Description
Optional[pd.DataFrame]

The Pandas DataFrame loaded from the provided file path, otherwise None if the

Optional[pd.DataFrame]

file does not exists.

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

A CSV-file reader returning a Pandas DataFrame and displayed as a table in Streamlit.

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]]]

Path to the CSV file. CSV file must exists, even for the Streamlit mode.

required
label Optional[str]

Label to display on top of the table.

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
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

import pandas as pd
from onecode import csv_reader, Mode, Project

Project().mode = Mode.EXECUTE
widget = csv_reader(
    key="CsvReader",
    value="/path/to/file.csv",
    label="My CSV Reader",
    tags=['CSV']
)

pd.testing.assert_frame_equal(widget, pd.read_csv("/path/to/file.csv"))

_validate(value)

Raises:

Type Description
ValueError

if the DataFrame is empty.

streamlit(id)

Returns:

Type Description
str

The Streamlit code for a Pandas DataFrame (st.dataframe).

Note

A file selector (st.file_uploader) is provided on top of the table.