Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hack for matplotlib support #26

Merged
merged 2 commits into from
Nov 28, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions environment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,5 @@ channels:
dependencies:
- xeus-python=0.0.1
- notebook
- ipywidgets>=7.4
- itkwidgets
141 changes: 62 additions & 79 deletions notebooks/xeus-python.ipynb
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"cell_type": "markdown",
"metadata": {},
"outputs": [],
"source": [
"from ipywidgets import IntSlider"
"<center>\n",
" <img src=\"xeus-python.png\" width=\"50%\">\n",
" <h1>Python kernel based on xeus</h1>\n",
"</center>"
]
},
{
Expand All @@ -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"
]
},
{
Expand All @@ -42,7 +26,7 @@
"metadata": {},
"outputs": [],
"source": [
"slider"
"a"
]
},
{
Expand All @@ -51,7 +35,7 @@
"metadata": {},
"outputs": [],
"source": [
"slider.value = 36"
"a += 6"
]
},
{
Expand All @@ -60,9 +44,7 @@
"metadata": {},
"outputs": [],
"source": [
"from ipywidgets import Video\n",
"video = Video.from_file(\"Big.Buck.Bunny.mp4\")\n",
"video"
"print(a)"
]
},
{
Expand All @@ -71,8 +53,7 @@
"metadata": {},
"outputs": [],
"source": [
"from ipywidgets import Widget\n",
"Widget.close_all()"
"a"
]
},
{
Expand All @@ -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)"
]
},
{
Expand All @@ -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)"
]
},
{
Expand All @@ -107,8 +85,7 @@
"metadata": {},
"outputs": [],
"source": [
"a = 3\n",
"a"
"print(a)"
]
},
{
Expand All @@ -117,7 +94,7 @@
"metadata": {},
"outputs": [],
"source": [
"a"
"import missing_module"
]
},
{
Expand All @@ -126,7 +103,7 @@
"metadata": {},
"outputs": [],
"source": [
"a += 6"
"b = {\"a\": 2, \"b\": 3}"
]
},
{
Expand All @@ -135,7 +112,7 @@
"metadata": {},
"outputs": [],
"source": [
"print(a)"
"b"
]
},
{
Expand All @@ -144,7 +121,7 @@
"metadata": {},
"outputs": [],
"source": [
"a"
"print(b[\"a\"])"
]
},
{
Expand All @@ -153,7 +130,11 @@
"metadata": {},
"outputs": [],
"source": [
"?"
"import time\n",
"\n",
"for x in range(5):\n",
" print(x)\n",
" time.sleep(1)"
]
},
{
Expand All @@ -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"
]
},
{
Expand All @@ -187,7 +161,7 @@
"metadata": {},
"outputs": [],
"source": [
"print(a)"
"from ipywidgets import IntSlider"
]
},
{
Expand All @@ -196,7 +170,7 @@
"metadata": {},
"outputs": [],
"source": [
"import missing_module"
"slider = IntSlider()"
]
},
{
Expand All @@ -205,7 +179,7 @@
"metadata": {},
"outputs": [],
"source": [
"b = {\"a\": 2, \"b\": 3}"
"slider"
]
},
{
Expand All @@ -214,7 +188,7 @@
"metadata": {},
"outputs": [],
"source": [
"b"
"slider.value"
]
},
{
Expand All @@ -223,7 +197,7 @@
"metadata": {},
"outputs": [],
"source": [
"from ipywidgets import IntSlider"
"slider"
]
},
{
Expand All @@ -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"
]
},
{
Expand All @@ -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"
]
},
{
Expand All @@ -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)"
]
},
{
Expand Down
Binary file added notebooks/xeus-python.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
12 changes: 12 additions & 0 deletions src/xcomm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -141,12 +149,16 @@ namespace xpyt
.def_property_readonly("kernel", &xcomm::kernel);

m.def("register_target", &register_target);
m.def("register_post_execute", &register_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;
Expand Down