Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

self.html is of type str and has no id attribute when using TemplateResponse/TemplateStringResponse #599

Open
florian-rabis opened this issue Sep 13, 2024 · 3 comments

Comments

@florian-rabis
Copy link

florian-rabis commented Sep 13, 2024

Hi,
I'm having an issue when using TemplateResponse. Apparently, the template is not being translated into a Lona node tree.

The error is not thrown right at the beginning but only when I click the button. However, my open_dialog method is not executed.

I get this tracestack:

MainThread                     ERROR    10:02:32.873117 aiohttp.server Error handling request
  Traceback (most recent call last):
    File "C:\Users\user\Desktop\data\Workspace\python-lona-app\.venv\Lib\site-packages\aiohttp\web_protocol.py", line 456, in _handle_request
      resp = await request_handler(request)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    File "C:\Users\user\Desktop\data\Workspace\python-lona-app\.venv\Lib\site-packages\aiohttp\web_app.py", line 537, in _handle
      resp = await handler(request)
             ^^^^^^^^^^^^^^^^^^^^^^
    File "C:\Users\user\Desktop\data\Workspace\python-lona-app\.venv\Lib\site-packages\lona\server.py", line 649, in _shielded_handle_http_request
      return await asyncio.shield(self._handle_http_request(request))
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    File "C:\Users\user\Desktop\data\Workspace\python-lona-app\.venv\Lib\site-packages\lona\server.py", line 597, in _handle_http_request
      return await self._handle_websocket_request(http_request)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    File "C:\Users\user\Desktop\data\Workspace\python-lona-app\.venv\Lib\site-packages\lona\server.py", line 508, in _handle_websocket_request
      await self._handle_websocket_message(connection, message)
    File "C:\Users\user\Desktop\data\Workspace\python-lona-app\.venv\Lib\site-packages\lona\server.py", line 453, in _handle_websocket_message
      await self._middleware_controller.handle_websocket_message(
    File "C:\Users\user\Desktop\data\Workspace\python-lona-app\.venv\Lib\site-packages\lona\middleware_controller.py", line 206, in handle_websocket_message
      return await self.server.run_function_async(
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    File "C:\Users\user\AppData\Local\Programs\Python\Python312\Lib\concurrent\futures\thread.py", line 58, in run
      result = self.fn(*self.args, **self.kwargs)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    File "C:\Users\user\Desktop\data\Workspace\python-lona-app\.venv\Lib\site-packages\lona\server.py", line 711, in _function
      return function(*args, **kwargs)
             ^^^^^^^^^^^^^^^^^^^^^^^^^
    File "C:\Users\user\Desktop\data\Workspace\python-lona-app\.venv\Lib\site-packages\lona\middleware_controller.py", line 111, in _run_middlewares_sync
      return_value = hook(data)
                     ^^^^^^^^^^
    File "C:\Users\user\Desktop\data\Workspace\python-lona-app\.venv\Lib\site-packages\lona\middlewares\lona_messages.py", line 32, in handle_websocket_message
      data.server._view_runtime_controller.handle_lona_message(
    File "C:\Users\user\Desktop\data\Workspace\python-lona-app\.venv\Lib\site-packages\lona\view_runtime_controller.py", line 321, in handle_lona_message
      self.handle_input_event_message(
    File "C:\Users\user\Desktop\data\Workspace\python-lona-app\.venv\Lib\site-packages\lona\view_runtime_controller.py", line 121, in handle_input_event_message
      view_runtime.handle_input_event(
    File "C:\Users\user\Desktop\data\Workspace\python-lona-app\.venv\Lib\site-packages\lona\view_runtime.py", line 715, in handle_input_event
      input_event = InputEvent(
                    ^^^^^^^^^^^
    File "C:\Users\user\Desktop\data\Workspace\python-lona-app\.venv\Lib\site-packages\lona\events\input_event.py", line 64, in __init__
      self.nodes = document.get_node(node_id=self.node_info[0])
                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    File "C:\Users\user\Desktop\data\Workspace\python-lona-app\.venv\Lib\site-packages\lona\html\document.py", line 40, in get_node
      if self.html.id == node_id:
         ^^^^^^^^^^^^
  AttributeError: 'str' object has no attribute 'id'

Here is my code:

views.py:

class IndexView(View):
    def open_dialog(self, input_event):
        print("Hello World")

    def handle_request(self, request):
        self.catalog_button = Button("Catalog", handle_click=self.open_dialog)
        self.menu.nodes = [
            self.catalog_button
        ]

        return TemplateResponse(
            "base.html", 
            {
                "menu": self.menu,
                "content": self.content
            }
        )

templates/base.html:

<header>
    <h1>Apptitle</h1>
</header>

<nav>
    {{ menu }}
</nav>

<main>
    {{ content }}
</main>

<footer></footer>

I use the picocss frontend template. When I just return all as HTML-Node then everything works fine.

When I modify the document.py like this, it seems to work correctly:

document.py:

...

def get_node(self, node_id):
    node = None
    nodes = []

    with self.lock:
        if type(self.html) is str:
            self.html = html.parse_html(self.html)[0]
        
        if self.html.id == node_id:
            node = self.html

...

Best regards

@fscherf
Copy link
Member

fscherf commented Sep 13, 2024

hi @florian-rabis!

Template responses are not intended to be used like this. You use either Lona nodes or a template in a view, but not both.

@florian-rabis
Copy link
Author

Thank you for the explanation. OK, I’ll need to rethink my approach a bit. I thought it would be more efficient to create as few nodes as possible server-side as Python objects. My idea was to be able to adjust lists or tables more flexibly using templates.

@fscherf
Copy link
Member

fscherf commented Oct 9, 2024

No problem! Don't hesitate to ask if something remains unclear.

I wouldn't worry too much about that. Lona is designed to handle big trees with a lot of nodes. If you encounter any bottlenecks, let me know.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants