bernhard-42 / jupyter-cadquery

An extension to render cadquery objects in JupyterLab via pythreejs
Apache License 2.0
312 stars 44 forks source link

Incremental show object not working? #49

Closed Rage997 closed 3 years ago

Rage997 commented 3 years ago

Hi,

If I try to import the functions show and show object as shown in the README:

from jupyter_cadquery.viewer.client import show, show_object

The code prints the following lines in console without displaying anything to the sidecar:

. sending ... Reconnecting to server… Resending ... Reconnecting to server… Resending .

The show works correctly if imported as follows: from jupyter_cadquery.viewer.client import show

However, this module has no show_object() function implemented. And this line fails: from jupyter_cadquery.viewer.client import show, show_object

I really want to use the incremental version of show() which is the mentioned show_object(). What should I do?

bernhard-42 commented 3 years ago

@Rage997

1) Juypter Cadquery in Jupyterlab

If you are using Juypter Cadquery just in Jupyter, there is currently no built-in function for incremental viewing. But it is easy to add:

import cadquery as cq
from jupyter_cadquery.cadquery import show
from jupyter_cadquery import set_sidecar

set_sidecar("Test", init=True)
objects = []

def show_object(obj):
    global objects
    objects.append(obj)
    show(*objects)

Then

s = cq.Workplane().sphere(1.2)
show_object(s)

and

b = cq.Workplane().box(1,2,3)
show_object(b)

Reset by setting objects = [] again

2) Juypter CadQuery viewer

If you use the viewer, there is no sidecar. It is a different approach, to use Jupyter CadQuery from an IDE:

Start jcv:

$ jcv
[JCV] Creating a Jupyter kernel specification called 'jcv' for this conda environment
Installed kernelspec jcv in /usr/local/share/jupyter/kernels/jcv
[JCV] Copying the voila notebook to /Users/bernhard/.jcv
[JCV] Signing the volia notebook
Signing notebook: /Users/bernhard/.jcv/viewer.ipynb
[JCV] Starting Jupyter CadQuery Viewer
[Voila] Using /var/folders/n5/9s5nr_8n79b7w2ly9n2cqcvh0000gn/T to store connection files
[Voila] Storing connection files in /var/folders/n5/9s5nr_8n79b7w2ly9n2cqcvh0000gn/T/voila_74hn_f05.
[Voila] Serving static files from /opt/miniconda/envs/jcq22/lib/python3.8/site-packages/voila/static.
[Voila] Voilà is running at:
http://mymac:8866/
Opening in existing browser session.
[Voila] Kernel started: 69c7e6f0-3491-422f-8cc2-51bd2ef1af80
WARNING:tornado.access:404 GET /voila/files/favicon.ico (127.0.0.1) 1.86ms

Your browser should point to localhost:8866 and then the following should work from an ipython shell or an IDE:

from jupyter_cadquery.viewer.client import show, show_object, reset
s = cq.Workplane().sphere(1.2)
b = cq.Workplane().box(1,2,3)

reset() # in case you want to view the next object on a clean canvas

show_object(s)
# . sending ... done

show_object(b)
# .. sending ... done

I just tried with version 2.2.0