flet python can't get the value of a dynamic dropdown

493 Views Asked by At

I have a list of items for my dropdown created with flet

dropdown_options = ["np.int_", "np.str_", "np.float_"]

This is no the problem, the problem is when I'm trying to get the value. But first let me share my code where I'm adding the items:

def on_dialog_file_picker_result(e: ft.FilePickerResultEvent): global dropdown_options if file_picker.result != None and file_picker.result.files != None: columnsList = [] columnsNames = []

    for f in file_picker.result.files:
        print(f"File: {f.name}")
        print(f"Path: {f.path}")

        # Manage csv
        dataframe = dd.read_csv(f.path, low_memory=False, dtype=str, encoding='latin-1')
        # Get columns names
        for colName in dataframe.columns:
            # save the column name in the list
            columnsNames.append(colName)
            # Add Column to the list for the row
            columnsList.append(ft.Text(value=colName, size=12, color=ft.colors.BLACK))
            # Add Dropdown datatypea
            dtypes = ft.Dropdown(width=100,
                                 label="dtype",
                                 options=[ft.dropdown.Option(option) for option in dropdown_options],
                                 on_change=lambda e, col=colName: dropdown_changed(e, col))

            columnsList.append(dtypes)

    row = Row(spacing=10, controls = list(columnsList))

    page.add(row)
    page.update()

Exactly this is the code:

# Add Dropdown datatypea
            dtypes = ft.Dropdown(width=100,
                                 label="dtype",
                                 options=[ft.dropdown.Option(option) for option in dropdown_options],
                                 on_change=lambda e, col=colName: dropdown_changed(e, col))

            columnsList.append(dtypes)

As we can see there, I'm adding this items dynamically inside a for loop.

And this is the code on the on_change event for the dropdown:

def dropdown_changed(e, column_name):
    selected_option = e.value
    selected_value = dropdown_options[selected_option]
    print(f"Dropdown for column {column_name} changed to {selected_value}")

    # Perform any additional actions based on the selected value

    page.update()

The items of the dropdown after a pick a csv file by using the file picker, this are added without problem, the problem is how I'm trying to get the value of the dropdown:

I'm getting this error:

Traceback (most recent call last):
  File "C:\Users\fredd\AppData\Local\Programs\Python\Python310\lib\threading.py", line 1009, in _bootstrap_inner
    self.run()
  File "C:\Users\fredd\AppData\Local\Programs\Python\Python310\lib\threading.py", line 946, in run
    self._target(*self._args, **self._kwargs)
  File "C:\Users\fredd\PycharmProjects\bigCsvImporter\app.py", line 67, in <lambda>
    on_change=lambda e, col=colName: dropdown_changed(e, col))
  File "C:\Users\fredd\PycharmProjects\bigCsvImporter\app.py", line 37, in dropdown_changed
    selected_option = e.value
AttributeError: 'ControlEvent' object has no attribute 'value'

This error happens after trying to select an item of the dropdown:

enter image description here

I would like to fix this problem guys, I need this value to set to my dataframe the datatype for every column, because by default all the columns are setted as "str".

thanks in advance.

0

There are 0 best solutions below