Repository navigation
Allow show to write the screenshot to a file-like object - #2136
lucas-santos-f wants to merge 2 commits into
Conversation
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #2136 +/- ##
==========================================
+ Coverage 95.81% 95.86% +0.05%
==========================================
Files 30 30
Lines 9612 9657 +45
Branches 1446 1461 +15
==========================================
+ Hits 9210 9258 +48
+ Misses 247 245 -2
+ Partials 155 154 -1 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
screenshot now also takes a binary file-like object such as io.BytesIO. The PNG is then written to memory with vtkPNGWriter.WriteToMemoryOn and copied into it, which avoids a temporary file when the image is only needed in memory (docs, notebooks). Fixes CadQuery#1760
645a2af to
e21a3c2
Compare
|
Done: it checks for BytesIO with isinstance now, and the type hint and docs say BytesIO. |
jmwright
left a comment
There was a problem hiding this comment.
Thanks @lucas-santos-f just one comment.
From the PR description:
"The check is hasattr(screenshot, "write") rather than isinstance(screenshot, str) because VTK also takes a pathlib.Path in SetFileName, and that should keep working."
However, the code seems to be the inverse of that. Is that correct, and if so, is it intentional?
|
Yes, that's intentional, the description was out of date, sorry. The first version checked |
Fixes #1760
show(..., screenshot=...)now also takes a binary file-like object such asio.BytesIO. In that casevtkPNGWriterwrites to memory (WriteToMemoryOn) and the PNG bytes are written to the object, so docs or notebooks can get the image without going through a temporary file:A file name works as before: only a
BytesIOgoes to memory (isinstance(screenshot, BytesIO)), and anything else, astror apathlib.Path, still goes toSetFileName. The example above is added to the Screenshots section ofdoc/vis.rst.The new
test_screenshot_to_memoryuses thepatch_vtkfixture, but feeds the realvtkPNGWritera 10x5 image fromvtkImageCanvasSource2Din place of the window, and checks the PNG signature and the size in the IHDR chunk. On master it fails withTypeError: SetFileName argument 1.Checked locally (Windows 10, Python 3.12, pip install with cadquery-ocp 8.0.1.1, VTK 9.6.2):
BytesIOare identical to the file written withscreenshot="img.png"pytest -n 8: 904 passed, 3 skipped (master: 903 passed, 3 skipped), no new warningsblack . --diff --check(cq fork): nothing to reformatmypy cadquery: same errors as master; here they are almost allimport-untypedfor OCP, since the pip package has no stubsHappy to adjust anything if needed, e.g. if you'd rather have
showreturn the bytes.AI disclosure: this change was written with the help of Claude Code (Claude Opus 5.5).