posit-dev / posit-dev/py-shiny
Shiny Express errors when trying to render `DuckDBPyConnection` object
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 1.8k
- Forks
- 135
- Avg merge
- 2d 18h
- Merged PRs (30d)
- 21
Description
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.
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Reproduce the linked app, then start with shiny/express/_run.py around run_express and follow the htmltools tag-conversion path shown in the traceback. Compare the proposed handling options and confirm that the top-level con.register example no longer crashes, with the resulting behavior and error guidance clearly documented.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- pandas, python
- Domain
- frontend
- Issue type
- Bug
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 35/100