{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import subprocess\nimport sys\n\nif \"google.colab\" in sys.modules:\n subprocess.run(\"apt-get update\", shell=True, check=True)\n subprocess.run(\"apt-get install -qq xvfb libgl1-mesa-glx\", shell=True, check=True)\n subprocess.run(\"pip install pyvista[all] -qq\", shell=True, check=True)\n\n import pyvista as pv\n\n # Seems that only static plotting is supported by colab at the moment\n pv.global_theme.jupyter_backend = \"static\"\n pv.global_theme.notebook = True\n pv.start_xvfb()\nelse:\n %matplotlib inline\n from pyvista import set_plot_theme\n\n set_plot_theme(\"document\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Checkbox Widget {#checkbox_widget_example}\n===============\n\nUse a checkbox to turn on/off the visibility of meshes in a scene.\n\nSee `pyvista.Plotter.add_checkbox_button_widget`{.interpreted-text\nrole=\"func\"} for more details.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import pyvista as pv" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Single Checkbox\n===============\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "mesh = pv.Sphere()\n\np = pv.Plotter()\nactor = p.add_mesh(mesh)\n\n\ndef toggle_vis(flag):\n actor.SetVisibility(flag)\n\n\np.add_checkbox_button_widget(toggle_vis, value=True)\np.show()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "And here is a screen capture of a user interacting with this\n\n![image](../../images/gifs/single-checkbox-widget.gif)\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Multiple Checkboxes\n===================\n\nIn this example, we will add many meshes to a scene with unique colors\nand create corresponding checkboxes for those meshes of the same color\nto toggle their visibility in the scene.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "colors = [\n [\"ff0000\", \"28e5da\", \"0000ff\"],\n [\"ffff00\", \"c8bebe\", \"f79292\"],\n [\"fffff0\", \"f18c1d\", \"23dcaa\"],\n [\"d785ec\", \"9d5b13\", \"e4e0b1\"],\n [\"894509\", \"af45f5\", \"fff000\"],\n]\n\n\nclass SetVisibilityCallback:\n \"\"\"Helper callback to keep a reference to the actor being modified.\"\"\"\n\n def __init__(self, actor):\n self.actor = actor\n\n def __call__(self, state):\n self.actor.SetVisibility(state)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "# Widget size\nsize = 50\n\np = pv.Plotter()\n\nStartpos = 12\nfor i, lst in enumerate(colors):\n for j, color in enumerate(lst):\n actor = p.add_mesh(pv.Sphere(center=(i, j, 0)), color=color)\n # Make a separate callback for each widget\n callback = SetVisibilityCallback(actor)\n p.add_checkbox_button_widget(\n callback,\n value=True,\n position=(5.0, Startpos),\n size=size,\n border_size=1,\n color_on=color,\n color_off='grey',\n background_color='grey',\n )\n Startpos = Startpos + size + (size // 10)\n\np.show()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "And here is a screen capture of a user interacting with this\n\n![image](../../images/gifs/multiple-checkbox-widget.gif)\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "```{=html}\n