Skip to content

Shiny Express errors when trying to render DuckDBPyConnection object #1771

Open
@wch

Description

@wch

The following app crashes on start because of the con.register() at the top level. Live app. (It could be suppressed by using with ui.hold():.)

from shiny.express import input, render, ui
import pandas as pd
import duckdb

# Create a sample DataFrame
df = pd.DataFrame({
    'id': range(1, 6),
    'name': ['Alice', 'Bob', 'Charlie', 'David', 'Eve'],
    'age': [25, 30, 35, 28, 22]
})


# Register DataFrame with DuckDB
con = duckdb.connect(':memory:')
con.register('people', df)

The error message is:

Traceback (most recent call last):
  File "<exec>", line 393, in _start_app
  File "<exec>", line 366, in __init__
  File "/lib/python3.12/site-packages/shiny/express/_run.py", line 62, in wrap_express_app
    app_module = importlib.import_module(package_name)
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/lib/python312.zip/importlib/__init__.py", line 90, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "<frozen importlib._bootstrap>", line 1387, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1360, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1331, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 935, in _load_unlocked
  File "/lib/python3.12/site-packages/shiny/express/_run.py", line 102, in exec_module
    module.app = create_express_app(  # pyright: ignore[reportAttributeAccessIssue]
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/lib/python3.12/site-packages/shiny/express/_run.py", line 132, in create_express_app
    app_ui = run_express(file, package_name).tagify()
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/lib/python3.12/site-packages/shiny/express/_run.py", line 231, in run_express
    get_top_level_recall_context_manager().__exit__(None, None, None)
  File "/lib/python3.12/site-packages/shiny/express/_recall_context.py", line 48, in __exit__
    res = self.fn(*self.args, **self.kwargs)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/lib/python3.12/site-packages/shiny/ui/_page.py", line 719, in page_auto
    return page_fn(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^
  File "/lib/python3.12/site-packages/shiny/ui/_page.py", line 775, in _page_auto_fixed
    return page_fixed(
           ^^^^^^^^^^^
  File "/lib/python3.12/site-packages/shiny/ui/_page.py", line 518, in page_fixed
    div({"class": "container"}, *args, **kwargs),
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/lib/python3.12/site-packages/htmltools/tags.py", line 731, in div
    return Tag("div", *args, _add_ws=_add_ws, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/lib/python3.12/site-packages/htmltools/_core.py", line 679, in __init__
    self.children = TagList(*kids)
                    ^^^^^^^^^^^^^^
  File "/lib/python3.12/site-packages/htmltools/_core.py", line 281, in __init__
    super().__init__(_tagchilds_to_tagnodes(args))
                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/lib/python3.12/site-packages/htmltools/_core.py", line 1936, in _tagchilds_to_tagnodes
    raise TypeError(
TypeError: Invalid tag item type: <class 'duckdb.duckdb.DuckDBPyConnection'>. Consider calling str() on this value before treating it as a tag item.

Shiny tries to render the object, but fails. A few possible ways of dealing with this:

  • We should have a more useful error message and suggest how to fix.
  • We could automatically not render objects that Shiny doesn't know how to render.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions