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

Consider a more robust exec approach #5

Open
bernhard-42 opened this issue Apr 3, 2022 · 17 comments
Open

Consider a more robust exec approach #5

bernhard-42 opened this issue Apr 3, 2022 · 17 comments

Comments

@bernhard-42
Copy link

exec fails with simple (an natural) approaches:

import cadquery as cq

l = 1
w = 2
h = 3

def box():
    return cq.Workplane().box(l, w, h).edges().fillet(0.1)

show(box())

fails with

  File "<string>", line 9, in box
NameError: name 'cq' is not defined

So let's change it (just for the test, I would never program like that):

l = 1
w = 2
h = 3


def box():
    import cadquery as cq

    return cq.Workplane().box(l, w, h).edges().fillet(0.1)


show(box())

Now, this fails with

  File "<string>", line 9, in box
NameError: name 'l' is not defined

exec just doesn't play well with global variables

Of course, this works:

def box(l=1, w=2, h=3):
    import cadquery as cq

    return cq.Workplane().box(l, w, h).edges().fillet(0.1)

show(box())

However I would need to adapt my style of coding - especially for quick hacks - to the limitations of exec.

Maybe there is a more robust way?

@njourdane
Copy link
Owner

It should be fixed in the last version of pip package / docker image. Do you want to give a try?

@bernhard-42
Copy link
Author

It behaves pretty strange:

I start with

l = 1
w = 2
h = 3

Then I change l, save, change w, save and change h and save. Looks good up to now.
However, when I press save then without changes, the view show kind of a random view of the four views I have created with my process, see animated gif, where I just press save before the view changes.

error

@njourdane
Copy link
Owner

njourdane commented Apr 15, 2022

I cannot not reproduce this. Do you use the docker version of cadquery-server or the pip one?

@bernhard-42
Copy link
Author

I use pip. I anyhow have cadquery installed and don't need the whole docker infrastructure on my Mac

@bernhard-42
Copy link
Author

... and the pip version allows to load step files. Not sure this is supported with docker where the interpreter runs in the container

@bernhard-42
Copy link
Author

It would be great if you could get rid of the server component. My viewer approach uses zmq. There is a version for Javascript: https://github.com/zeromq/zeromq.js It seems that the server component in javascript or typescript running in VS Code can receive messages from python zmq lib.
Then one would only need a show function that does the tessellation and then sends the tessellated arrays via zmq to VS Code.
Just an idea, since for me the server component is as inconvenient as my approach with the separate viewer.
And, with the zmq approach, one could use the Python debugger in VS Code to do step-by-step debugging of the code and every now and then call show to check the interims results.
Just my 2 cent ...

@njourdane
Copy link
Owner

I use pip. I anyhow have cadquery installed and don't need the whole docker infrastructure on my Mac

Yes, this is totally legit, I just want to understand how to reproduce the bug, I don't have it on the docker version.

Then one would only need a show function that does the tessellation and then sends the tessellated arrays via zmq to VS Code.

The solution with zmq is interesting. I made the choice of the server part because of these two benefits:

  • avoid complex installation: for many end-users, installing cadquery can be difficult, so I liked the idea to run a docker container that "just work";
  • reduce the rendering delay: since the cadquery server already loaded the cadquery module.

The first point could be eventually solved with CadQuery/cadquery#1048.
I don't see how to adress the second point with the zmq architecture. Do you?

@bernhard-42
Copy link
Author

bernhard-42 commented Apr 15, 2022

avoid complex installation

I agree, installing CadQuery is not always fun. I have spent quite some time on it in the past. So the docker approach is good, but is a little bit inflexible: CadQuery doesn't have a short term release strategy (2.1. is more than 1 year old and everyone works with the latest master) and with your docker container I am restricted to the version you provide. I cannot quickly pip install the git repo (If OCP doesn't change, pip of Cadquery repo is sufficient).

By the way, can I load e.g. step files with the docker version? Or save STL results for my 3d printer?

I personally don't really trust the exec approach in Python due to the locals, globals handling. To me it feels like one can mess up things easily with unclear side effects. Maybe it is just because I don't understand it enough ...

reduce the rendering delay: since the cadquery server already loaded the cadquery module.

I understand this issue. I solved it in my workflow by working in Jupyter. I import cadquery only once for a session and even better, really complex interims results can be calculated once and then used in the cells below without recalculation. Big benefit for my workflows. I don't want to open the IDE vs. Notebook discussion. Just explain my approach to avoid the loading times (latency is an issue in general, so my latest version of Jupyter CadQuery - nearly ready - will have caching for bounding box calculations and tessellation results and uses multiprocessing for large CAD assemblies. That is a much bigger performance booster than avoiding the import of cadquery. Note this is only available in Jupyter-CadQuery not in three-cad-viewer).

But agreed, it is a difficult decision, and one has to go one route ...

@njourdane
Copy link
Owner

njourdane commented Apr 16, 2022

By the way, can I load e.g. step files with the docker version? Or save STL results for my 3d printer?

Yes, using a docker volume: docker run --rm -d -v $(pwd):/home/cq -p 5000:5000 cadquery/cadquery-server, but I agree it's not very practical. But anyway cadquery can now be installed with pip, so the use of docker is not really necessary.

I agree that the server and exec approach is not perfect.

I could eventually give the choice between using the cadquery server or not.

@bernhard-42
Copy link
Author

One way to overcome the latency is to use "shift-enter" approach in VS Code.
Instead of just saving (very convenient with cadquery-server), one could use Ctrl-A + Shift-Enter (or Cmd-A + Shift-Enter on the Mac). This will incrementally run the code in VS Code in the attached Python interpreter.
Not as easy as your cadquery-server approach, but no exec needed and same speed. Additionally, one could leverage pre-calculated objects by reusing the variables and just selecting the rest of the code before pressing Shift Enter.
Again, no optimal solution, however you had asked about ideas how to avoid the import latency.

However, both the "cadquery-server" and the "shift-enter" approach do not allow step-by-step debugging.

@bernhard-42
Copy link
Author

At least from a debugging perspective, I don't feel that the import cadquery is a big issue (at least on my laptop, which admittedly is a fast machine)

debug

Also a "Run without Debugging" is really fast on my laptop:

run

Given that experience, on my laptop I definitely would prefer to work without cadquery-server and be able to properly
debug my code step by step

@njourdane
Copy link
Owner

njourdane commented Aug 31, 2022

I made some work on this!

It consists on a huge change on the cadquery-server behavior.

The exec approach is not used anymore, instead the server executes the CadQuery code and renders the model using three-cad-viewer. Finally, it includes a file watcher that reloads the Python code and update the web page when the file is updated:

cq-server_v0-2-1_demo.webm

This approach allows to debug the code in VSCode.
It also makes the project compatible with any IDE and any web browser.
And also allow a user to display the model in an other screen, or even in an other computer on the same local network.

In VSCode, the web page can be displayed using LivePreview extension (ctrl+shift+P -> Simple Browser: Show).
This might turn the whole VSCode extension deprecated, as all the job is made by cq-server.

I moved it in a dedicated repository and can be installed with pip install cadquery-server (it doesn't include cadquery itself, which is an extra-dependency).

Do you want to give a try? :)

@bernhard-42
Copy link
Author

Hi @roipoussiere
having been away on vacation for the last two weeks, I how wanted to give it a try.
Unfortunately minify-html 0.10.0 doesn't seem to exist for Mac M1, only 0.9.2.
So I haven't managed to install it. Is 0.10.0 necessary?

@bernhard-42
Copy link
Author

wilsonzlin/minify-html@29e7b1c doesn't sound promising

@bernhard-42
Copy link
Author

Cloned it and gave it a try:

  • On a Mac, port 5000 is used by ControlCenter, hence I use -p parameter
  • Somehow on my machine it doesn't detect code changes. I use a file test.py and started with cq-server -p 5001 -m test.py. However, when I change and save test.py nothing happens and I have to refresh the browser
  • I don't understand how I could use step-by-step debugging in VS Code

@njourdane
Copy link
Owner

njourdane commented Sep 12, 2022

So I haven't managed to install it. Is 0.10.0 necessary?

I think you can use minify html 0.9.2. And it's only used for export when --minify option is passed to cli.

I don't understand how I could use step-by-step debugging in VS Code

In fact I realized it's not possible currently, the issue has been reported here: njourdane/cadquery-server#27. The idea is to actually execute the script when using the debugger, instead of reloading it using cq-server.

@bernhard-42
Copy link
Author

@roipoussiere Just for information:
The main reason for me to switch to and IDE like VSCode is step-by-step debugging. I have now created my own VSCode extension, which is loosely coupled between Python and the Typescript VSCode extension: The extension starts a web server on port 3939 and the Python show command sends tessellated objects to this web server via HTTP POST. No external server that needs to be run separately.

  • The CadQuery viewer extension for VS Code can be installed from a vsix file and started via "Ctrl-k v" / "Cmd-k v" (check CadQuery settings in VS Code)
  • The show command can be installed by installing a wheel, show_object would need to be improved (args)

Both vsix and wheel can be found in https://github.com/bernhard-42/vscode-cadquery-viewer/releases at the moment.

This gif shows both, using "Run in Python Terminal" (very fast for small experimental code snippets) and proper "Debugging" with VSCode debugger:

cq-vscode

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

2 participants