{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", " \n", "

Python kernel based on xeus

\n", "
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Simple code execution" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "a = 3" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "a" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "b = 89\n", "\n", "def sq(x):\n", " return x * x\n", "\n", "sq(b)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "print" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Redirected streams" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import sys\n", "\n", "print(\"Error !!\", file=sys.stderr)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Error handling" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "scrolled": true }, "outputs": [], "source": [ "\"Hello\"\n", "\n", "def dummy_function():\n", " import missing_module" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "dummy_function()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Code completion" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### press `tab` to see what is available in `sys` module" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from sys import " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Code inspection" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### using the question mark" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "?print" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### by pressing `shift+tab`" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "print(" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Input support" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "name = input('Enter your name: ')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "'Hello, ' + name" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Rich representation" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "class Person:\n", " def __init__(self, name=\"John Doe\", address=\"Paris\", picture=\"\"):\n", " self.name = name\n", " self.address = address\n", " self.picture = picture\n", "\n", " def _repr_mimebundle_(self, include=None, exclude=None):\n", " return {\n", " \"text/html\": \"\"\"\n", "
: {}
\n", "
: {}
\"\"\".format(self.picture, self.name, self.address) \n", " }" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "james = Person(\"James Smith\", \"Boston\")\n", "display(james)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "marie = Person(\"Marie Curie\", \"Poland\", \"./marie.png\")\n", "display(marie)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Matplotlib" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "%matplotlib inline" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import matplotlib.pyplot as plt\n", "import numpy as np" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "scrolled": true }, "outputs": [], "source": [ "fig = plt.figure()\n", "plt.plot(np.sin(np.linspace(0, 20, 100)));" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "%matplotlib widget" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import matplotlib.pyplot as plt\n", "import numpy as np" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "fig = plt.figure()\n", "plt.plot(np.sin(np.linspace(0, 20, 100)));" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Widgets support" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Basic widgets" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from ipywidgets import IntSlider" ] }, { "cell_type": "code", "execution_count": null, "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" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "slider" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "slider.value = 36" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Widget interacts" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from ipywidgets import interact" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "@interact\n", "def foo(x = ['a', 'b'], n=(1, 10)):\n", " print(x * n)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Binary buffers support for widgets" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from ipywidgets import Video\n", "video = Video.from_file(\"Big.Buck.Bunny.mp4\")\n", "video" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Higher-level widgets libraries support" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import matplotlib\n", "matplotlib.use(\"agg\")\n", "\n", "try:\n", " from urllib.request import urlretrieve\n", "except ImportError:\n", " from urllib import urlretrieve\n", "import os\n", "\n", "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)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## IPython.display module" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from IPython.display import clear_output, display, update_display\n", "from time import sleep" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Update display" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "class Square:\n", " color = 'PeachPuff'\n", " def _repr_html_(self):\n", " return '''\n", "
\n", "
''' % self.color\n", "square = Square()\n", "\n", "display(square, display_id='some-square')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "square.color = 'OliveDrab'\n", "update_display(square, display_id='some-square')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Clear output" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "print(\"hello\")\n", "sleep(3)\n", "clear_output() # will flicker when replacing \"hello\" with \"goodbye\"\n", "print(\"goodbye\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "print(\"hello\")\n", "sleep(3)\n", "clear_output(wait=True) # prevents flickering\n", "print(\"goodbye\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Display classes" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from IPython.display import HTML\n", "HTML('''\n", "
\n", "
''')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from IPython.display import Math\n", "Math(r'F(k) = \\int_{-\\infty}^{\\infty} f(x) e^{2\\pi i k} dx')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from IPython.display import Latex\n", "Latex(r\"\"\"\\begin{eqnarray}\n", "\\nabla \\times \\vec{\\mathbf{B}} -\\, \\frac1c\\, \\frac{\\partial\\vec{\\mathbf{E}}}{\\partial t} & = \\frac{4\\pi}{c}\\vec{\\mathbf{j}} \\\\\n", "\\nabla \\cdot \\vec{\\mathbf{E}} & = 4 \\pi \\rho \\\\\n", "\\nabla \\times \\vec{\\mathbf{E}}\\, +\\, \\frac1c\\, \\frac{\\partial\\vec{\\mathbf{B}}}{\\partial t} & = \\vec{\\mathbf{0}} \\\\\n", "\\nabla \\cdot \\vec{\\mathbf{B}} & = 0 \n", "\\end{eqnarray}\"\"\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from IPython.display import SVG\n", "SVG(url='https://jupyter.org/assets/main-logo.svg')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from IPython.display import SVG\n", "SVG(filename='./logo.svg')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from time import sleep\n", "from IPython.display import ProgressBar\n", "\n", "for i in ProgressBar(10):\n", " sleep(0.1)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from IPython.display import JSON\n", "JSON(['foo', {'bar': ('baz', None, 1.0, 2)}], metadata={}, expanded=True, root='test')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from IPython.display import GeoJSON\n", "GeoJSON(\n", " data={\n", " \"type\": \"Feature\",\n", " \"geometry\": {\n", " \"type\": \"Point\",\n", " \"coordinates\": [11.8, -45.04]\n", " }\n", " }, url_template=\"http://s3-eu-west-1.amazonaws.com/whereonmars.cartodb.net/{basemap_id}/{z}/{x}/{y}.png\",\n", " layer_options={\n", " \"basemap_id\": \"celestia_mars-shaded-16k_global\",\n", " \"attribution\" : \"Celestia/praesepe\",\n", " \"tms\": True,\n", " \"minZoom\" : 0,\n", " \"maxZoom\" : 5\n", " }\n", ")" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3.9 (XPython)", "language": "python", "name": "xpython" }, "language_info": { "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "version": "3.9.1" } }, "nbformat": 4, "nbformat_minor": 4 }