Created
December 7, 2015 22:00
-
-
Save bollwyvl/e6c0f5858d7d8d65830b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"# Loading STL from the Web" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [], | |
"source": [ | |
"from ipywidgets import Text, HBox, Button, HTML, Dropdown\n", | |
"from k3d import K3D\n", | |
"from IPython.display import display\n", | |
"import requests" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"K3D supports the ASCII format for STL, but the binary format is more popular on sites like [Thingiverse](https://www.thingiverse.com/). [numpy-stl](https://github.com/WoLpH/numpy-stl) supports reading and writing both formats. You can install it with `pip`:" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"!pip install numpy-stl" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"from stl.stl import StlMesh, ASCII\n", | |
"from tempfile import NamedTemporaryFile" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"plot = K3D()\n", | |
"stl = None" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"In addition to the plot, let's use some widgets and events to handle loading things from the web with `requests`." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [], | |
"source": [ | |
"thing = Text(placeholder=\"URL of STL\", value=\"http://www.thingiverse.com/download:99668\")\n", | |
"btn = Button(icon=\"fa-cloud-download\", button_style=\"primary\", description=\" Load\")\n", | |
"status = HTML()\n", | |
"\n", | |
"@btn.on_click\n", | |
"def load(_):\n", | |
" global stl, plot\n", | |
" btn.button_style = \"warning\"\n", | |
" btn.icon = \"fa-spinner fa-spin\"\n", | |
" btn.description = \" Loading...\"\n", | |
" status.value = \"\"\n", | |
" \n", | |
" if stl is not None:\n", | |
" plot - stl\n", | |
"\n", | |
" try:\n", | |
" with NamedTemporaryFile(delete=True) as tmp:\n", | |
" r = requests.get(thing.value, stream=True)\n", | |
" [tmp.write(chunk) for chunk in r.iter_content(chunk_size=1024)]\n", | |
" mesh = StlMesh(filename=tmp.name)\n", | |
" with NamedTemporaryFile(delete=True) as tmp:\n", | |
" mesh.save(tmp.name, mode=ASCII)\n", | |
" stl = K3D.stl(tmp.read())\n", | |
" plot += stl\n", | |
" btn.icon = \"fa-cloud-download\"\n", | |
" btn.button_style = \"success\"\n", | |
" btn.description = \" Loaded!\"\n", | |
" except Exception as err:\n", | |
" btn.icon = \"fa-exclamation-triangle\"\n", | |
" btn.button_style = \"danger\"\n", | |
" btn.description = \" Failed!\"\n", | |
" status.value = \"<pre>{}</pre>\".format(err)\n", | |
" raise err" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"The K3D widget can't go inside the standard layout widgets, but by `display`ing two widgets in the same cell, you can emulate a VBox. Try pressing the Load button, or copy/pasting some other examples:\n", | |
"- [castanets](http://www.thingiverse.com/download:99668)\n", | |
"- [python logo](https://www.thingiverse.com/download:1420085)\n", | |
"- [klein bottle](http://www.thingiverse.com/download:862590)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [], | |
"source": [ | |
"display(HBox(children=[thing, btn, status]))\n", | |
"display(plot)" | |
] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "Python 2", | |
"language": "python", | |
"name": "python2" | |
}, | |
"language_info": { | |
"codemirror_mode": { | |
"name": "ipython", | |
"version": 2 | |
}, | |
"file_extension": ".py", | |
"mimetype": "text/x-python", | |
"name": "python", | |
"nbconvert_exporter": "python", | |
"pygments_lexer": "ipython2", | |
"version": "2.7.10" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 0 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment