flet-dev / flet

Flet enables developers to easily build realtime web, mobile and desktop apps in Python. No frontend experience required.
https://flet.dev
Apache License 2.0
11.43k stars 445 forks source link

Not able to Inherit Row Control [User Code was incorrect: Wrong Arguments] #3020

Closed adikpb closed 7 months ago

adikpb commented 7 months ago

Description

When sub-classing a flet.Row, and calling self.page.update result in an error and no updation occurring. I was making an editable text display, that sub-classes from Row and has its controls set to either flet.Text and flet.IconButton or flet.TextField and flet.IconButton. Clicking on the icon button results in the state being changed to the either two mentioned before, during which self.page.update() is called, but calling self.page.update() results in an error stemming from flet_core (according to the error logs).

Code example to reproduce the issue:

import flet

class EditableText(flet.Row):
    def __init__(
        self,
        obj: flet.Control,
        value_attribute: str,
        theme_style: flet.TextThemeStyle,
        initial_value: str = None,
        **kwargs,
    ):
        super().__init__(alignment=flet.MainAxisAlignment.CENTER, spacing=0)

        self.obj: flet.Control = obj
        self.value_attribute: str = value_attribute

        self.label = flet.Text(
            # value=getattr(self.obj, self.value_attribute, None),
            theme_style=theme_style,
            **kwargs,
        )
        self.entry = flet.TextField(
            # value=getattr(self.obj, self.value_attribute, None),
            dense=True,
            on_submit=self.change_text,
            content_padding=0,
            text_style=theme_style,
        )
        self.action_button = flet.IconButton(
            icon=flet.icons.EDIT, on_click=self.edit_text
        )
        self.controls = [self.label, self.action_button]

    def edit_text(self, e):
        self.action_button.icon = flet.icons.CHECK
        self.action_button.on_click = self.change_text
        self.controls[0] = self.entry
        self.page.update()

    def change_text(self, e):
        self.action_button.icon = flet.icons.EDIT
        self.action_button.on_click = self.edit_text
        self.label.value = self.entry.value.strip()
        # setattr(self.obj, self.value_attribute, self.entry.value.strip())
        self.controls[0] = self.label
        self.page.update()

def main(page: flet.Page):
    page.add(EditableText("Anon", "emoty", theme_style=flet.TextThemeStyle.BODY_MEDIUM))

app = flet.app(main)

Describe the results you received:

No change observed visually on the page.

Error

Future exception was never retrieved
future: <Future finished exception=AttributeError("'mappingproxy' object has no attribute '__dict__'")>
Traceback (most recent call last):
  File "/usr/local/Cellar/python@3.12/3.12.3/Frameworks/Python.framework/Versions/3.12/lib/python3.12/concurrent/futures/thread.py", line 58, in run
    result = self.fn(*self.args, **self.kwargs)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/[REDACTED]/Desktop/Coding/Python/Python3.12/flet/dt/.venv/lib/python3.12/site-packages/flet_core/page.py", line 528, in wrapper
    handler(*args)
  File "/Users/[REDACTED]/Desktop/Coding/Python/Python3.12/flet/test.py", line 39, in edit_text
    self.page.update()
  File "/Users/[REDACTED]/Desktop/Coding/Python/Python3.12/flet/dt/.venv/lib/python3.12/site-packages/flet_core/page.py", line 324, in update
    r = self.__update(self)
        ^^^^^^^^^^^^^^^^^^^
  File "/Users/[REDACTED]/Desktop/Coding/Python/Python3.12/flet/dt/.venv/lib/python3.12/site-packages/flet_core/page.py", line 426, in __update
    commands, added_controls, removed_controls = self.__prepare_update(*controls)
                                                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/[REDACTED]/Desktop/Coding/Python/Python3.12/flet/dt/.venv/lib/python3.12/site-packages/flet_core/page.py", line 439, in __prepare_update
    control.build_update_commands(
  File "/Users/[REDACTED]/Desktop/Coding/Python/Python3.12/flet/dt/.venv/lib/python3.12/site-packages/flet_core/control.py", line 442, in build_update_commands
    ctrl.build_update_commands(
  File "/Users/[REDACTED]/Desktop/Coding/Python/Python3.12/flet/dt/.venv/lib/python3.12/site-packages/flet_core/control.py", line 442, in build_update_commands
    ctrl.build_update_commands(
  File "/Users/[REDACTED]/Desktop/Coding/Python/Python3.12/flet/dt/.venv/lib/python3.12/site-packages/flet_core/control.py", line 424, in build_update_commands
    innerCmds = ctrl._build_add_commands(
                ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/[REDACTED]/Desktop/Coding/Python/Python3.12/flet/dt/.venv/lib/python3.12/site-packages/flet_core/control.py", line 512, in _build_add_commands
    command = self._build_command(False)
              ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/[REDACTED]/Desktop/Coding/Python/Python3.12/flet/dt/.venv/lib/python3.12/site-packages/flet_core/control.py", line 541, in _build_command
    self.before_update()
  File "/Users/[REDACTED]/Desktop/Coding/Python/Python3.12/flet/dt/.venv/lib/python3.12/site-packages/flet_core/textfield.py", line 301, in before_update
    super().before_update()
  File "/Users/[REDACTED]/Desktop/Coding/Python/Python3.12/flet/dt/.venv/lib/python3.12/site-packages/flet_core/form_field_control.py", line 177, in before_update
    self._set_attr_json("textStyle", self.__text_style)
  File "/Users/[REDACTED]/Desktop/Coding/Python/Python3.12/flet/dt/.venv/lib/python3.12/site-packages/flet_core/control.py", line 140, in _set_attr_json
    nv = self._convert_attr_json(value)
         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/[REDACTED]/Desktop/Coding/Python/Python3.12/flet/dt/.venv/lib/python3.12/site-packages/flet_core/control.py", line 146, in _convert_attr_json
    json.dumps(value, cls=EmbedJsonEncoder, separators=(",", ":"))
  File "/usr/local/Cellar/python@3.12/3.12.3/Frameworks/Python.framework/Versions/3.12/lib/python3.12/json/__init__.py", line 238, in dumps
    **kw).encode(obj)
          ^^^^^^^^^^^
  File "/Users/[REDACTED]/Desktop/Coding/Python/Python3.12/flet/dt/.venv/lib/python3.12/site-packages/flet_core/embed_json_encoder.py", line 43, in encode
    return super().encode(self._convert_enums(o))
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/Cellar/python@3.12/3.12.3/Frameworks/Python.framework/Versions/3.12/lib/python3.12/json/encoder.py", line 200, in encode
    chunks = self.iterencode(o, _one_shot=True)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/Cellar/python@3.12/3.12.3/Frameworks/Python.framework/Versions/3.12/lib/python3.12/json/encoder.py", line 258, in iterencode
    return _iterencode(o, 0)
           ^^^^^^^^^^^^^^^^^
  File "/Users/[REDACTED]/Desktop/Coding/Python/Python3.12/flet/dt/.venv/lib/python3.12/site-packages/flet_core/embed_json_encoder.py", line 40, in default
    return self._convert_enums(obj.__dict__)
                               ^^^^^^^^^^^^
AttributeError: 'mappingproxy' object has no attribute '__dict__'. Did you mean: '__dir__'?

Describe the results you expected:

TextField to appear when the button is clicked.

Flet version (pip show flet):

Name: flet
Version: 0.22.0
Summary: Flet for Python - easily build interactive multi-platform apps in Python
Home-page: 
Author: Appveyor Systems Inc.
Author-email: hello@flet.dev
License: Apache-2.0
Location: /Users/bijoykozhampurath/Desktop/Coding/Python/Python3.12/flet/dt/.venv/lib/python3.12/site-packages
Requires: cookiecutter, fastapi, flet-runtime, packaging, qrcode, uvicorn, watchdog
Required-by: 

Give your requirements.txt file (don't pip freeze, instead give direct packages):

[tool.poetry]
package-mode = false

[tool.poetry.dependencies]
python = "^3.12"
flet = "*"

[build-system]
requires = ["poetry-core"]
build-backend = "poetry.core.masonry.api"

Operating system: MacOS Monetrey 12.7.4 (21H1123)

Additional environment details:

I am using poetry to manage virtual environments and dependencies.

poetry debug info

Poetry
Version: 1.8.2
Python:  3.12.3

Virtualenv
Python:         3.12.3
Implementation: CPython
Path:           /Users/bijoykozhampurath/Desktop/Coding/Python/Python3.12/flet/dt/.venv
Executable:     /Users/bijoykozhampurath/Desktop/Coding/Python/Python3.12/flet/dt/.venv/bin/python
Valid:          True

Base
Platform:   darwin
OS:         posix
Python:     3.12.3
Path:       /usr/local/opt/python@3.12/Frameworks/Python.framework/Versions/3.12
Executable: /usr/local/opt/python@3.12/Frameworks/Python.framework/Versions/3.12/bin/python3.12
adikpb commented 7 months ago

Sorry the issue wasnt with row but rather, the wrong argument being given to textfield for text_style.