I have a newbie Taipy question related to file_selector/callbacks.
I want to use the file_selector to load a CSV file. Here is what I am currently doing:
contents = """
<|file_selector|label=Upload dataset|on_action=load_csv_file|extensions=.csv|>
"""
def load_csv_file(selection):
dataset = pd.read_csv(selection)
However, the selection is not the selected file, but an (empty) State object… Am I missing something? How should I get the selected path so that I can do something with it?
You can found below a small code snippet to help you utilize this control effectively.
The file_selector is linked to a Python variable (here, path). This variable represents the path of the selected file in the file_selector. The current value of path can be accessed through the State object, as shown below. The State object holds all GUI variables used by the application. For more information, refer to the documentation here.
When a file is uploaded via the file_selector, the load_csv_file function is executed. The selected file's path, state.path
, is used to read the CSV.
from taipy.gui import Gui
import pandas as pd
path = None
data = None
md = """
<|{path}|file_selector|label=Upload dataset|on_action=load_csv_file|extensions=.csv|>
<|{data}|table|rebuild|>
"""
def load_csv_file(state):
state.data = pd.read_csv(state.path)
print(state.data)
Gui(md).run()