diff --git a/environment.yml b/environment.yml index 9ceceb75..b913c4e6 100644 --- a/environment.yml +++ b/environment.yml @@ -5,3 +5,5 @@ channels: dependencies: - xeus-python=0.0.1 - notebook + - ipywidgets>=7.4 + - itkwidgets diff --git a/notebooks/xeus-python.ipynb b/notebooks/xeus-python.ipynb index 3cbf59e2..c14bc2c6 100644 --- a/notebooks/xeus-python.ipynb +++ b/notebooks/xeus-python.ipynb @@ -1,12 +1,13 @@ { "cells": [ { - "cell_type": "code", - "execution_count": null, + "cell_type": "markdown", "metadata": {}, - "outputs": [], "source": [ - "from ipywidgets import IntSlider" + "
\n", + " \n", + "

Python kernel based on xeus

\n", + "
" ] }, { @@ -15,25 +16,8 @@ "metadata": {}, "outputs": [], "source": [ - "slider = IntSlider()" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "slider" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "slider.value" + "a = 3\n", + "a" ] }, { @@ -42,7 +26,7 @@ "metadata": {}, "outputs": [], "source": [ - "slider" + "a" ] }, { @@ -51,7 +35,7 @@ "metadata": {}, "outputs": [], "source": [ - "slider.value = 36" + "a += 6" ] }, { @@ -60,9 +44,7 @@ "metadata": {}, "outputs": [], "source": [ - "from ipywidgets import Video\n", - "video = Video.from_file(\"Big.Buck.Bunny.mp4\")\n", - "video" + "print(a)" ] }, { @@ -71,8 +53,7 @@ "metadata": {}, "outputs": [], "source": [ - "from ipywidgets import Widget\n", - "Widget.close_all()" + "a" ] }, { @@ -81,9 +62,12 @@ "metadata": {}, "outputs": [], "source": [ - "from IPython.display import Image\n", - "im = Image(url=\"marie.png\")\n", - "im" + "b = 89\n", + "\n", + "def sq(x):\n", + " return x * x\n", + "\n", + "sq(b)" ] }, { @@ -92,13 +76,7 @@ "metadata": {}, "outputs": [], "source": [ - "from bqplot import pyplot as plt\n", - "import numpy as np\n", - "\n", - "plt.figure()\n", - "n= 100\n", - "plt.plot(np.arange(n), np.cumsum(np.random.randn(n) * 100.))\n", - "plt.show(display_toolbar=True)" + "sq(4)" ] }, { @@ -107,8 +85,7 @@ "metadata": {}, "outputs": [], "source": [ - "a = 3\n", - "a" + "print(a)" ] }, { @@ -117,7 +94,7 @@ "metadata": {}, "outputs": [], "source": [ - "a" + "import missing_module" ] }, { @@ -126,7 +103,7 @@ "metadata": {}, "outputs": [], "source": [ - "a += 6" + "b = {\"a\": 2, \"b\": 3}" ] }, { @@ -135,7 +112,7 @@ "metadata": {}, "outputs": [], "source": [ - "print(a)" + "b" ] }, { @@ -144,7 +121,7 @@ "metadata": {}, "outputs": [], "source": [ - "a" + "print(b[\"a\"])" ] }, { @@ -153,7 +130,11 @@ "metadata": {}, "outputs": [], "source": [ - "?" + "import time\n", + "\n", + "for x in range(5):\n", + " print(x)\n", + " time.sleep(1)" ] }, { @@ -162,23 +143,16 @@ "metadata": {}, "outputs": [], "source": [ - "b = 89\n", - "\n", - "display(im)\n", - "\n", - "def test(x):\n", - " return x * 2\n", + "import sys\n", "\n", - "test(b)" + "print(\"Error !!\", file=sys.stderr)" ] }, { - "cell_type": "code", - "execution_count": null, + "cell_type": "markdown", "metadata": {}, - "outputs": [], "source": [ - "test(4)" + "# Widgets support" ] }, { @@ -187,7 +161,7 @@ "metadata": {}, "outputs": [], "source": [ - "print(a)" + "from ipywidgets import IntSlider" ] }, { @@ -196,7 +170,7 @@ "metadata": {}, "outputs": [], "source": [ - "import missing_module" + "slider = IntSlider()" ] }, { @@ -205,7 +179,7 @@ "metadata": {}, "outputs": [], "source": [ - "b = {\"a\": 2, \"b\": 3}" + "slider" ] }, { @@ -214,7 +188,7 @@ "metadata": {}, "outputs": [], "source": [ - "b" + "slider.value" ] }, { @@ -223,7 +197,7 @@ "metadata": {}, "outputs": [], "source": [ - "from ipywidgets import IntSlider" + "slider" ] }, { @@ -232,16 +206,14 @@ "metadata": {}, "outputs": [], "source": [ - "IntSlider()" + "slider.value = 36" ] }, { - "cell_type": "code", - "execution_count": null, + "cell_type": "markdown", "metadata": {}, - "outputs": [], "source": [ - "print(b[\"a\"])" + "## Binary buffers support for widgets" ] }, { @@ -250,20 +222,16 @@ "metadata": {}, "outputs": [], "source": [ - "print(b[\"c\"])" + "from ipywidgets import Video\n", + "video = Video.from_file(\"Big.Buck.Bunny.mp4\")\n", + "video" ] }, { - "cell_type": "code", - "execution_count": null, + "cell_type": "markdown", "metadata": {}, - "outputs": [], "source": [ - "import time\n", - "\n", - "for x in range(10):\n", - " print(x)\n", - " time.sleep(1)" + "## Higher-level widgets libraries support" ] }, { @@ -272,9 +240,24 @@ "metadata": {}, "outputs": [], "source": [ - "import sys\n", + "try:\n", + " from urllib.request import urlretrieve\n", + "except ImportError:\n", + " from urllib import urlretrieve\n", + "import os\n", "\n", - "print(\"Error !!\", file=sys.stderr)" + "import itk\n", + "\n", + "from itkwidgets import view\n", + "\n", + "# Download data\n", + "file_name = '005_32months_T2_RegT1_Reg2Atlas_ManualBrainMask_Stripped.nrrd'\n", + "if not os.path.exists(file_name):\n", + " url = 'https://data.kitware.com/api/v1/file/564a5b078d777f7522dbfaa6/download'\n", + " urlretrieve(url, file_name)\n", + "\n", + "image = itk.imread(file_name)\n", + "view(image)" ] }, { diff --git a/notebooks/xeus-python.png b/notebooks/xeus-python.png new file mode 100644 index 00000000..b1ac64de Binary files /dev/null and b/notebooks/xeus-python.png differ diff --git a/src/xcomm.cpp b/src/xcomm.cpp index caba61dd..e01252cb 100644 --- a/src/xcomm.cpp +++ b/src/xcomm.cpp @@ -109,6 +109,14 @@ namespace xpyt }; } + void register_post_execute(py::args, py::kwargs) + { + } + + void enable_gui(py::args, py::kwargs) + { + } + void register_target(py::str target_name, py::object callback) { auto target_callback = [target_name, callback] (xeus::xcomm&& comm, const xeus::xmessage& msg) { @@ -141,12 +149,16 @@ namespace xpyt .def_property_readonly("kernel", &xcomm::kernel); m.def("register_target", ®ister_target); + m.def("register_post_execute", ®ister_post_execute); + m.def("enable_gui", &enable_gui); m.def("get_kernel", [m] () { py::object xeus_python = m.attr("_Mock"); py::object kernel = m.attr("_Mock"); py::object comm_manager = m.attr("_Mock"); + xeus_python.attr("register_post_execute") = m.attr("register_post_execute"); + xeus_python.attr("enable_gui") = m.attr("enable_gui"); comm_manager.attr("register_target") = m.attr("register_target"); kernel.attr("comm_manager") = comm_manager; xeus_python.attr("kernel") = kernel;