Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to load a CSV file using file_selector and callbacks in Taipy

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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

md = "<|{path}|file_selector|label=Upload dataset|on_action=load_csv_file|extensions=.csv|>"

def load_csv_file(state):
    data = pd.read_csv(state.path)
    print(data)

Gui(md).run()
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading