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

Debugger widget inside template suppresses errors, breaks binding, and after reload show 'AttributeError: 'NoneType' object has no attribute 'document' 500 Server Error #7436

Open
1 task done
jnareb opened this issue Oct 24, 2024 · 1 comment

Comments

@jnareb
Copy link

jnareb commented Oct 24, 2024

ALL software version info

Software Version Info
Python 3.12.4

panel==1.5.2

bokeh==3.6.0
ipykernel==6.29.5
ipython==8.27.0
jupyter_bokeh==4.0.5
jupyter_client==8.6.3
jupyter_core==5.7.2
jupyter_server==2.14.2
jupyter_server_terminals==0.5.3
jupyterlab==4.2.5
jupyterlab_pygments==0.3.0
jupyterlab_server==2.27.3
jupyterlab_widgets==3.0.13

Google Chrome 129.0.6668.100 (64-bit)

Description of expected behavior and the observed behavior

I tried to add Debugger widget to a template-based Panel application (with widgets in the sidebar, and panels in the main pane).

I have created a simple application, with a widget, two bound function (one using the widget, one using the other bound function), and a simple Markdown pane. Each of the functions writes to logs using logger.debug on entry. The idea was to compare using pn.bind with pn.rx.

When I used the Debugger widget as shown in the documentation, that is simply in the cell, everything works correctly.

  • changing the selected in the Select widget changes the Markdown pane output
  • debug information appears in the Debugging widget, in the appropriate cell output

When I added MaterialTemplate with widget in sidebar, and Markdown widget in the main pane, and made that template .servable();, and then previewed with 'Preview with Panel' (and with panel serve), everything still worked correctly.

However, when I added the Debugger to the main pane, the application stopped working correctly. It either did not update the Markdown pane (bound functions do not work), or produced 500 Server Error.

In the first case, in the 'Serve with Panel':

  • changing the selected value didn't change the output
  • nothing appeared in the Debugger widget

In the second case I got the following strange error:

AttributeError: 'NoneType' object has no attribute 'document'

Often reloading the incorrectly working (but displayed) page lead to showing the error above. Further reloading have always shown the error.

Complete, minimal, self-contained example code that reproduces the issue

# notebooks/panel/00-panel-Debugger-demo.ipynb
import logging

import panel as pn


#pn.extension('terminal', console_output='disable')
pn.extension('terminal')

# testing the Debugger widget
debug = pn.widgets.Debugger(
    name='Debugger (level=DEBUG)',
    only_last=False,
    level=logging.DEBUG, logger_names=['panel.Debugger'])
debug

logger.debug("DEBUG")
logger.info("INFO")
logger.warning("WARNING")
logger.error("ERROR")


widget = pn.widgets.Select(name='Select', options=['Biology', 'Chemistry', 'Physics'])

def markdown_contents(value):
    logger.debug(f"markdown_contents({value=})")
    return f"""Value is _{value}_"""

def append_exclamation(value: str) -> str:
    logger.debug(f"append_exclamation({value=})")
    return value + "!!!"

bound_markdown_contents = pn.bind(
    markdown_contents, widget,
)
bound_append_exclamation = pn.bind(
    append_exclamation, bound_markdown_contents,
)

# testing that bounding works correctly - it does
pn.Column(widget, pn.pane.Str(bound_append_exclamation))

template = pn.template.MaterialTemplate(
    site="debugger",
    title="Debugger demo",
    sidebar_width=300,
    sidebar=[
        widget,
    ],
    main=[
        pn.pane.Markdown(bound_append_exclamation),
        pn.pane.Str(bound_append_exclamation),
    ],
)
template.main.extend([
    # NOTE: with the line below commented out, everything works just fine
    debug,
])
template.servable(); # The ; is needed in the notebook to not display the template. Its not needed in a script

Stack traceback and/or browser JavaScript console output

Stack traceback after reloading

AttributeError: 'NoneType' object has no attribute 'document'

Traceback (most recent call last):
  File "~/.venv/lib/python3.12/site-packages/panel/io/handlers.py", line 405, in run
    exec(self._code, module.__dict__)
  File "~/notebooks/panel/00-panel-Debugger-demo.ipynb", line 30, in <module>
    "metadata": {},
  File "/usr/lib/python3.12/logging/__init__.py", line 1527, in debug
    self._log(DEBUG, msg, args, **kwargs)
  File "/usr/lib/python3.12/logging/__init__.py", line 1684, in _log
    self.handle(record)
  File "/usr/lib/python3.12/logging/__init__.py", line 1700, in handle
    self.callHandlers(record)
  File "/usr/lib/python3.12/logging/__init__.py", line 1762, in callHandlers
    hdlr.handle(record)
  File "/usr/lib/python3.12/logging/__init__.py", line 1022, in handle
    rv = self.filter(record)
         ^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.12/logging/__init__.py", line 858, in filter
    result = f.filter(record)
             ^^^^^^^^^^^^^^^^
  File "~/.venv/lib/python3.12/site-packages/panel/widgets/debugger.py", line 97, in filter
    widget_session_ids = set(m.document.session_context.id
                         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "~/.venv/lib/python3.12/site-packages/panel/widgets/debugger.py", line 99, in <genexpr>
    tuple()) if m.document.session_context)
                ^^^^^^^^^^
AttributeError: 'NoneType' object has no attribute 'document'
  • I may be interested in making a pull request to address this
@jnareb
Copy link
Author

jnareb commented Oct 24, 2024

This looks like re-broken issue #5235 (supposedly fixed in v1.4.0, but present in v1.5.2). The same work-around of wrapping Debugger in some layout, like pn.Row or pn.Card works.

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

1 participant