widgetti / solara

A Pure Python, React-style Framework for Scaling Your Jupyter and Web Apps
https://solara.dev
MIT License
1.91k stars 141 forks source link

ipycanvas doesn't work with solara #444

Open edublancas opened 10 months ago

edublancas commented 10 months ago

I want to develop an app that uses solara and ipycanvas, but it seems like ipycanvas isn't supported.

App code:

import solara
from ipycanvas import Canvas

@solara.component
def Page():
    canvas = Canvas(width=200, height=200)
    return canvas

Then, I start the app:

solara run app.py

And I get this error:

Traceback (most recent call last):
  File "/Users/eduardo/miniconda3/envs/tmp/lib/python3.11/site-packages/reacton/core.py", line 1675, in _render
    root_element = el.component.f(*el.args, **el.kwargs)
                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/eduardo/Desktop/jupyai-demos/app.py", line 58, in Page
    canvas = RoughCanvas(width=width, height=height)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/eduardo/miniconda3/envs/tmp/lib/python3.11/site-packages/ipycanvas/canvas.py", line 620, in __init__
    super(Canvas, self).__init__(*args, **kwargs)
  File "/Users/eduardo/miniconda3/envs/tmp/lib/python3.11/site-packages/ipywidgets/widgets/widget.py", line 506, in __init__
    self.open()
  File "/Users/eduardo/miniconda3/envs/tmp/lib/python3.11/site-packages/ipywidgets/widgets/widget.py", line 525, in open
    state, buffer_paths, buffers = _remove_buffers(self.get_state())
                                                   ^^^^^^^^^^^^^^^^
  File "/Users/eduardo/miniconda3/envs/tmp/lib/python3.11/site-packages/ipywidgets/widgets/widget.py", line 615, in get_state
    value = to_json(getattr(self, k), self)
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/eduardo/miniconda3/envs/tmp/lib/python3.11/site-packages/ipywidgets/widgets/widget.py", line 54, in _widget_to_json
    return "IPY_MODEL_" + x.model_id
                          ^^^^^^^^^^
  File "/Users/eduardo/miniconda3/envs/tmp/lib/python3.11/site-packages/solara/server/patch.py", line 367, in model_id_debug
    raise RuntimeError(f"Widget has been closed, the stacktrace when the widget was closed is:\n{closed_stack[id(self)]}")
RuntimeError: Widget has been closed, the stacktrace when the widget was closed is:
  File "/Users/eduardo/miniconda3/envs/tmp/bin/solara", line 8, in <module>
    sys.exit(main())
  File "/Users/eduardo/miniconda3/envs/tmp/lib/python3.11/site-packages/solara/__main__.py", line 706, in main
    cli()
  File "/Users/eduardo/miniconda3/envs/tmp/lib/python3.11/site-packages/click/core.py", line 1157, in __call__
    return self.main(*args, **kwargs)
  File "/Users/eduardo/miniconda3/envs/tmp/lib/python3.11/site-packages/rich_click/rich_command.py", line 126, in main
    rv = self.invoke(ctx)
  File "/Users/eduardo/miniconda3/envs/tmp/lib/python3.11/site-packages/click/core.py", line 1688, in invoke
    return _process_result(sub_ctx.command.invoke(sub_ctx))
  File "/Users/eduardo/miniconda3/envs/tmp/lib/python3.11/site-packages/click/core.py", line 1434, in invoke
    return ctx.invoke(self.callback, **ctx.params)
  File "/Users/eduardo/miniconda3/envs/tmp/lib/python3.11/site-packages/click/core.py", line 783, in invoke
    return __callback(*args, **kwargs)
  File "/Users/eduardo/miniconda3/envs/tmp/lib/python3.11/site-packages/solara/__main__.py", line 428, in run
    start_server()
  File "/Users/eduardo/miniconda3/envs/tmp/lib/python3.11/site-packages/solara/__main__.py", line 400, in start_server
    server.run()
  File "/Users/eduardo/miniconda3/envs/tmp/lib/python3.11/site-packages/uvicorn/server.py", line 61, in run
    return asyncio.run(self.serve(sockets=sockets))
  File "/Users/eduardo/miniconda3/envs/tmp/lib/python3.11/asyncio/runners.py", line 190, in run
    return runner.run(main)
  File "/Users/eduardo/miniconda3/envs/tmp/lib/python3.11/asyncio/runners.py", line 118, in run
    return self._loop.run_until_complete(task)
  File "/Users/eduardo/miniconda3/envs/tmp/lib/python3.11/asyncio/base_events.py", line 640, in run_until_complete
    self.run_forever()
  File "/Users/eduardo/miniconda3/envs/tmp/lib/python3.11/asyncio/base_events.py", line 607, in run_forever
    self._run_once()
  File "/Users/eduardo/miniconda3/envs/tmp/lib/python3.11/asyncio/base_events.py", line 1922, in _run_once
    handle._run()
  File "/Users/eduardo/miniconda3/envs/tmp/lib/python3.11/asyncio/events.py", line 80, in _run
    self._context.run(self._callback, *self._args)
  File "/Users/eduardo/miniconda3/envs/tmp/lib/python3.11/site-packages/uvicorn/server.py", line 68, in serve
    config.load()
  File "/Users/eduardo/miniconda3/envs/tmp/lib/python3.11/site-packages/uvicorn/config.py", line 467, in load
    self.loaded_app = import_from_string(self.app)
  File "/Users/eduardo/miniconda3/envs/tmp/lib/python3.11/site-packages/uvicorn/importer.py", line 21, in import_from_string
    module = importlib.import_module(module_str)
  File "/Users/eduardo/miniconda3/envs/tmp/lib/python3.11/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1204, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1176, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1147, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 690, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 940, in exec_module
  File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
  File "/Users/eduardo/miniconda3/envs/tmp/lib/python3.11/site-packages/solara/server/starlette.py", line 47, in <module>
    from . import app as appmod
  File "<frozen importlib._bootstrap>", line 1232, in _handle_fromlist
  File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
  File "<frozen importlib._bootstrap>", line 1176, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1147, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 690, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 940, in exec_module
  File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
  File "/Users/eduardo/miniconda3/envs/tmp/lib/python3.11/site-packages/solara/server/app.py", line 407, in <module>
    apps["__default__"] = AppScript(os.environ.get("SOLARA_APP", "solara.website.pages:Page"))
  File "/Users/eduardo/miniconda3/envs/tmp/lib/python3.11/site-packages/solara/server/app.py", line 79, in __init__
    dummy_kernel_context.close()
  File "/Users/eduardo/miniconda3/envs/tmp/lib/python3.11/site-packages/solara/server/kernel_context.py", line 92, in close
    widgets.Widget.close_all()
  File "/Users/eduardo/miniconda3/envs/tmp/lib/python3.11/site-packages/ipywidgets/widgets/widget.py", line 351, in close_all
    widget.close()
  File "/Users/eduardo/miniconda3/envs/tmp/lib/python3.11/site-packages/solara/server/patch.py", line 387, in close_widget_debug
    stacktrace = "".join(traceback.format_stack())

Is this expected? Are there any workarounds?

iisakkirotko commented 10 months ago

Hi @edublancas!

It does seem like there are some issues with ipycanvas in solara. I'm using ipycanvas 0.12.1, since higher versions don't seem compatible at all.

There's a hidden example on using ipycanvas here... Although I checked and it doesn't seem to be working as is, so I'll be taking a look at fixing things.

Interestingly, this code

import solara
import ipycanvas
from reacton import ipycanvas as c
from reacton import ipywidgets as w

count = solara.reactive(0)

@solara.component
def Page():
    solara.Button(f"Draw {count.value}", on_click=lambda: count.set(count.value + 1))

    def draw():
        print(canvas_element)
        canvas = solara.get_widget(canvas_element)
        print(canvas)

        with ipycanvas.hold_canvas(canvas):
            canvas.fill_style = "#63934e"
            canvas.fill_rect(10 * count.value, 10 * count.value, 10, 10)

    solara.use_effect(draw, [count.value])

    canvas_element = c.Canvas(
        width=700,
        height=500,
    )
    print(canvas_element)

Page = Page()
Page

runs fine if you execute it through a notebook cell (solara run app.ipynb), but not if you run it as a standalone app.

edublancas commented 10 months ago

awesome, thanks for looking at this!

in case it helps, there are some notes in the docs about why ipycanvas doesn't work out of the box in Voila (perhaps they're related to why they don't work on Solara?): https://ipycanvas.readthedocs.io/en/latest/advanced.html#ipycanvas-in-voila