Skip to content

Instantly share code, notes, and snippets.

@oeway
Last active January 29, 2021 15:18
Show Gist options
  • Save oeway/690c2e62311223ae93e644d542eb8949 to your computer and use it in GitHub Desktop.
Save oeway/690c2e62311223ae93e644d542eb8949 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Use Kaibu in Jupyter Notebooks"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Setup\n",
"\n",
"Before start the demos here, you need to install the imjoy jupyter extension:\n",
"```\n",
"pip install -U imjoy-jupyter-extension\n",
"```\n",
"\n",
"Then **restart your jupyter notebook**, and open a new notebook and you should **see an ImJoy icon in the title bar**."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Hello world for ImJoy"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from imjoy import api\n",
"\n",
"class ImJoyPlugin():\n",
" async def setup(self):\n",
" pass\n",
"\n",
" async def run(self, ctx):\n",
" await api.alert(\"hello world\")\n",
"\n",
"api.export(ImJoyPlugin())"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Meet Kaibu!"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from imjoy import api\n",
"\n",
"class ImJoyPlugin():\n",
" async def setup(self):\n",
" pass\n",
"\n",
" async def run(self, ctx):\n",
" viewer = await api.showDialog(src=\"https://kaibu.org/#/app\")\n",
"\n",
"api.export(ImJoyPlugin())"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Show an image"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from imjoy import api\n",
"import numpy as np\n",
"import asyncio\n",
"# import imageio\n",
"\n",
"class ImJoyPlugin():\n",
" async def setup(self):\n",
" pass\n",
"\n",
" async def run(self, ctx):\n",
" viewer = await api.createWindow(src=\"https://kaibu.org/#/app\")\n",
"\n",
" # create a random image\n",
" image = np.random.randint(0, 255, [500, 500], dtype='uint8')\n",
" \n",
" # or you can try if you also did `pip install imageio` and `import imageio`\n",
" # image = imageio.imread(\"https://images.proteinatlas.org/19661/221_G2_1_red_green.jpg\")\n",
"\n",
" # view image\n",
" await viewer.view_image(image, type=\"itk-vtk\", name=\"random pixels\")\n",
"\n",
"api.export(ImJoyPlugin())"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Add polygons and points"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"scrolled": false
},
"outputs": [],
"source": [
"from imjoy import api\n",
"import numpy as np\n",
"import asyncio\n",
"# import imageio\n",
"\n",
"class ImJoyPlugin():\n",
" async def setup(self):\n",
" pass\n",
"\n",
" async def run(self, ctx):\n",
" viewer = await api.createWindow(src=\"https://kaibu.org/#/app\")\n",
"\n",
" # create a random image\n",
" image = np.random.randint(0, 255, [500, 500], dtype='uint8')\n",
" \n",
" # or you can try if you also did `pip install imageio` and `import imageio`\n",
" # image = imageio.imread(\"https://images.proteinatlas.org/19661/221_G2_1_red_green.jpg\")\n",
"\n",
" # view image\n",
" await viewer.view_image(image, type=\"itk-vtk\", name=\"random pixels\")\n",
" \n",
" # add polygon to a vector layer\n",
" triangle = np.array([[11, 13], [111, 113], [22, 246]], dtype='uint16')\n",
" await viewer.add_shapes([ triangle ], shape_type=\"polygon\", edge_color=\"red\", name=\"triangle\")\n",
"\n",
" # add points to a vector layer\n",
" points = np.random.randint(0, 500, [100, 2], dtype='uint16')\n",
" await viewer.add_points(points, face_color=\"purple\", name=\"points\")\n",
" \n",
"\n",
"api.export(ImJoyPlugin())"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Show a 3D volume"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"\n",
"async def run():\n",
" viewer = await api.createWindow(src=\"https://kaibu.org/#/app\")\n",
"\n",
" volume = np.random.randint(0, 255, [30, 30, 30], dtype='uint8')\n",
"\n",
" # view image\n",
" await viewer.view_image(volume, type=\"itk-vtk\", name=\"3D volume\")\n",
"\n",
"asyncio.ensure_future(run())"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Add a custom UI buttons"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"\n",
"async def run():\n",
" viewer = await api.createWindow(src=\"https://kaibu.org/#/app\")\n",
"\n",
" image = np.random.randint(0, 255, [500, 500], dtype='uint8')\n",
"\n",
" # view image\n",
" await viewer.view_image(image, type=\"itk-vtk\", name=\"random pixels\")\n",
"\n",
" points = np.random.randint(0, 500, [100, 2], dtype='uint16')\n",
" layer = await viewer.add_points(points, face_color=\"red\")\n",
"\n",
" async def say_hello():\n",
" await api.alert('Hello!')\n",
"\n",
" async def get_geojson_features():\n",
" # get the annotation in geojson format\n",
" features = await layer.get_features()\n",
" await api.alert(str(features))\n",
"\n",
" await viewer.set_ui({\"title\": \"Utilities\",\n",
" \"elements\": [\n",
" {\"_rintf\": True,\n",
" \"type\": \"button\",\n",
" \"label\": \"Say Hello\",\n",
" \"callback\": say_hello\n",
" },\n",
" {\"_rintf\": True,\n",
" \"type\": \"button\",\n",
" \"label\": \"Show GeoJSON\",\n",
" \"callback\": get_geojson_features\n",
" },\n",
" \n",
" ]\n",
" })\n",
"\n",
"asyncio.ensure_future(run())"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.10"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
numpy
pandas
scipy
matplotlib
ipywidgets>=7.0.0
imageio
scikit-image
imjoy>=0.10.0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment