{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Objekte anzeigen mit `display`\n", "\n", "IPython kann Objekte anzeigen wie z.B. `HTML`, `JSON`, `PNG`, `JPEG`, `SVG` und `Latex`. " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Bilder\n", "\n", "Um Bilder (`JPEG`, `PNG`) in IPython und Notebooks anzuzeigen, könnt ihr die `Image`-Klasse verwenden:" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "execution": { "iopub.execute_input": "2026-05-21T20:58:55.845046Z", "iopub.status.busy": "2026-05-21T20:58:55.844791Z", "iopub.status.idle": "2026-05-21T20:58:55.956844Z", "shell.execute_reply": "2026-05-21T20:58:55.956101Z", "shell.execute_reply.started": "2026-05-21T20:58:55.845027Z" } }, "outputs": [ { "data": { "image/gif": "R0lGODlh0wBHAPcAAP/////78vf3///35fb19fPy8u/v7//tyPHs2uvq6urq6vXoxv/tYObm5vHlw//krP/rXvXmpvXjmP/mWf/gl9zh5/bfoP/jVN7e3vfekvjhbt3d3friYv/gUtfe483f7vbYl/XZjfXagv/We//Wg//bTNbW1vfUe//YSf/TdvjVaPfShPnWW/vVT//VRP/PasbU4P/QQP/NV/fNdMLR3vvOR/jNV//MO8zMzPjHYvvIQPzFOf/FMsXFxfnCRLDH2fnAScLCwvvAN/q+QP2+Lv+8Kb29vf+4Lf+1Mf+1IZu/3Zu92qG6z5260bW1taysrJCvyYKv1KioqIKt0aOjo3+oyqGgoICmxn6lxoCjwH2hv3+euJmZmXKgxZGRkXCWtZCPj2SVvWSRtVqRv2SOsoaGhlKNvX5+fkmIu0SJwFCFsEmFtkOFuziFw0ODtjiCvkl/rFB8oUN/snV1dTh/ukJ9rjh8tUN5pzd5r2xsbDd2q0JzpTxynjdzpWZmZjZtnDdqlP///wAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA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CMSXXpgygdXgojrIqEaFwNGOWhGe3MGBF3bUzIQkaUlecEICHmaCkXrynwUJqECOqSiteMFGswwKFPumAo/0pQBAxYFQCUeQBAwVA0AFauSEOrOMmGCo9vzLQqZlSfe9r4IU6WBFFirVl8yxqlNsXh6xOhGtTmSl+utqTipautPZUQYUGIEmK2JWiWQnkGqtCR3r+L4BeCYFSSACXTNIET0NMa81aYHyZBhWHgTAMyRIAlklUleIhMoPeYgqYmeSASliMpMvOMADjpCEFQy2kBE5aSg3mxMWrJOdSYhtEnJgkcrSJsQET+CClOZAVNbCxAI1+CwegRlbJAjTInrqrUKKxSRn+pYnCzgBEIRABE0OYQYFcyoVQiiRHpSBCk7g7nPHS97ymve86E2vetfLXoYQIKnwja9829ve98oXqMUT3n2TSt/yEsC+Sc2vgPN73/6yFsD4LV4CFszgBieAwPE1cF7/C18FM7gBGM5wAxpcvPlKuKsILkB+HUxiCMP3wxMO8YBXrF8PozjFFN6vjJP63/++2L81zrGOd3zjHvv4x0AOspCHTOQiG/nISE6ykpfc44AAADs=", "text/plain": [ "" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from IPython.display import Image\n", "\n", "\n", "Image(\"https://www.python.org/images/python-logo.gif\")" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "execution": { "iopub.execute_input": "2026-05-21T20:58:55.957996Z", "iopub.status.busy": "2026-05-21T20:58:55.957671Z", "iopub.status.idle": "2026-05-21T20:58:56.020114Z", "shell.execute_reply": "2026-05-21T20:58:56.019647Z", "shell.execute_reply.started": "2026-05-21T20:58:55.957974Z" } }, "outputs": [ { "data": { "image/svg+xml": [ "\n", " \n", " \n", " \n", " image/svg+xml\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "" ], "text/plain": [ "" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from IPython.display import SVG\n", "\n", "\n", "SVG(\n", " \"https://www.python.org/static/community_logos/python-logo-generic.svg\",\n", ")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Nicht-eingebettete Bilder\n", "\n", "* Standardmäßig sind Bilddaten eingebettet:\n", "\n", " ``` Python\n", " Image ('img_url')\n", " ```\n", "\n", "* Wenn jedoch `url` als `kwarg` angegeben ist, wird dies als *Softlink* interpretiert:\n", "\n", " ``` Python\n", " Image (url='img_url')\n", " ```\n", "\n", "* `embed` kann jedoch auch explizit angegeben werden: \n", "\n", " ``` Python\n", " Image (url='img_url', embed = True)\n", " ```" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## HTML\n", "\n", "Python-Objekte können HTML-Repräsentationen deklarieren, die in einem Notebook angezeigt werden:" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "execution": { "iopub.execute_input": "2026-05-21T20:58:56.020637Z", "iopub.status.busy": "2026-05-21T20:58:56.020531Z", "iopub.status.idle": "2026-05-21T20:58:56.022736Z", "shell.execute_reply": "2026-05-21T20:58:56.022392Z", "shell.execute_reply.started": "2026-05-21T20:58:56.020629Z" } }, "outputs": [], "source": [ "from IPython.display import HTML" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "execution": { "iopub.execute_input": "2026-05-21T20:58:56.023095Z", "iopub.status.busy": "2026-05-21T20:58:56.023001Z", "iopub.status.idle": "2026-05-21T20:58:56.025526Z", "shell.execute_reply": "2026-05-21T20:58:56.025173Z", "shell.execute_reply.started": "2026-05-21T20:58:56.023088Z" } }, "outputs": [ { "data": { "text/html": [ "\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "%%html\n", "
    \n", "
  • foo
  • \n", "
  • bar
  • \n", "
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Javascript\n", "\n", "Mit Notebooks können Objekte auch eine JavaScript-Darstellung deklarieren. Dies ermöglicht dann z.B. Datenvisualisierungen mit Javascript-Bibliotheken wie [d3.js](https://d3js.org/)." ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "execution": { "iopub.execute_input": "2026-05-21T20:58:56.025864Z", "iopub.status.busy": "2026-05-21T20:58:56.025801Z", "iopub.status.idle": "2026-05-21T20:58:56.028455Z", "shell.execute_reply": "2026-05-21T20:58:56.027897Z", "shell.execute_reply.started": "2026-05-21T20:58:56.025858Z" } }, "outputs": [ { "data": { "application/javascript": [ "alert(\"An example of a JavaScript warning displayed by IPython.\")" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "from IPython.display import Javascript\n", "\n", "\n", "welcome = Javascript(\n", " 'alert(\"An example of a JavaScript warning displayed by IPython.\")',\n", ")\n", "display(welcome)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Für umfangreicheres Javascript könnt ihr auch die `%%javascript`-Syntax verwenden." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## LaTeX\n", "\n", "`IPython.display` verfügt außerdem über eine integrierte Unterstützung für die Anzeige von mathematischen Ausdrücken, die in LaTeX gesetzt sind und im Browser mit [MathJax](https://www.mathjax.org/) gerendert werden:" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "execution": { "iopub.execute_input": "2026-05-21T20:58:56.030188Z", "iopub.status.busy": "2026-05-21T20:58:56.030047Z", "iopub.status.idle": "2026-05-21T20:58:56.032459Z", "shell.execute_reply": "2026-05-21T20:58:56.032212Z", "shell.execute_reply.started": "2026-05-21T20:58:56.030174Z" } }, "outputs": [ { "data": { "text/latex": [ "$\\displaystyle F(k) = \\int_{-\\infty}^{\\infty} f(x) e^{2\\pi i k} dx$" ], "text/plain": [ "" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from IPython.display import Math\n", "\n", "\n", "Math(r\"F(k) = \\int_{-\\infty}^{\\infty} f(x) e^{2\\pi i k} dx\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Bei der `Latex`-Klasse müsst ihr die Begrenzungen selbst angeben. Auf diese Weise könnt ihr jedoch auch andere LaTeX-Modi verwenden, wie z.B. `eqnarray`:" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "execution": { "iopub.execute_input": "2026-05-21T20:58:56.032921Z", "iopub.status.busy": "2026-05-21T20:58:56.032844Z", "iopub.status.idle": "2026-05-21T20:58:56.035000Z", "shell.execute_reply": "2026-05-21T20:58:56.034784Z", "shell.execute_reply.started": "2026-05-21T20:58:56.032914Z" } }, "outputs": [ { "data": { "text/latex": [ "\\begin{eqnarray}\n", " \\nabla \\times \\vec{\\mathbf{B}} -\\, \\frac1c\\,\n", " \\frac{\\partial\\vec{\\mathbf{E}}} {\\partial t} & =\n", " \\frac{4\\pi}{c}\\vec{\\mathbf{j}} \\\\ \\end{eqnarray}" ], "text/plain": [ "" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from IPython.display import Latex\n", "\n", "\n", "Latex(\n", " r\"\"\"\\begin{eqnarray}\n", " \\nabla \\times \\vec{\\mathbf{B}} -\\, \\frac1c\\,\n", " \\frac{\\partial\\vec{\\mathbf{E}}} {\\partial t} & =\n", " \\frac{4\\pi}{c}\\vec{\\mathbf{j}} \\\\ \\end{eqnarray}\"\"\",\n", ")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Audio\n", "\n", "IPython ermöglicht auch das interaktive Arbeiten mit Sounds. Mit der `display.Audio`-Klasse könnt ihr ein Audio-Control erstellen, das in das Notebook eingebettet ist. Die Schnittstelle ist analog zu denjenigen der `Image`-Klasse. Alle vom Browser unterstützten Audioformate können verwendet werden." ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "execution": { "iopub.execute_input": "2026-05-21T20:58:56.035295Z", "iopub.status.busy": "2026-05-21T20:58:56.035238Z", "iopub.status.idle": "2026-05-21T20:58:56.036705Z", "shell.execute_reply": "2026-05-21T20:58:56.036507Z", "shell.execute_reply.started": "2026-05-21T20:58:56.035289Z" } }, "outputs": [], "source": [ "from IPython.display import Audio" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Im folgenden werden wir die Sinusfunktion eines NumPy-Array als Audiosignal ausgeben. Dabei normalisiert und codiert die `Audio`-Klasse die Daten und bettet das resultierende Audio in das Notebook ein." ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "execution": { "iopub.execute_input": "2026-05-21T20:58:56.037030Z", "iopub.status.busy": "2026-05-21T20:58:56.036951Z", "iopub.status.idle": "2026-05-21T20:58:56.064086Z", "shell.execute_reply": "2026-05-21T20:58:56.063738Z", "shell.execute_reply.started": "2026-05-21T20:58:56.037017Z" } }, "outputs": [ { "data": { "text/html": [ "\n", " \n", " " ], "text/plain": [ "" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import numpy as np\n", "\n", "\n", "f = 500.0\n", "rate = 8000\n", "L = 3\n", "times = np.linspace(0, L, rate * L)\n", "signal = np.sin(f * times)\n", "\n", "Audio(data=signal, rate=rate)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Links zu lokalen Dateien\n", "\n", "IPython bietet integrierte Klassen zum Generieren von Links zu lokalen Dateien. Erstellt hierzu eine Verknüpfung zu einer einzelnen Datei mit dem `FileLink`-Objekt:" ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "execution": { "iopub.execute_input": "2026-05-21T20:58:56.064607Z", "iopub.status.busy": "2026-05-21T20:58:56.064478Z", "iopub.status.idle": "2026-05-21T20:58:56.067193Z", "shell.execute_reply": "2026-05-21T20:58:56.066955Z", "shell.execute_reply.started": "2026-05-21T20:58:56.064597Z" } }, "outputs": [ { "data": { "text/html": [ "magics.ipynb
" ], "text/plain": [ "/Users/veit/cusy/trn/Python4DataScience-de/docs/workspace/ipython/magics.ipynb" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from IPython.display import FileLink, FileLinks\n", "\n", "\n", "FileLink(\"magics.ipynb\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Alternativ könnt ihr auch eine Liste mit Links zu allen Dateien in einem Verzeichnis generieren, z.B.:" ] }, { "cell_type": "code", "execution_count": 11, "metadata": { "execution": { "iopub.execute_input": "2026-05-21T20:58:56.067522Z", "iopub.status.busy": "2026-05-21T20:58:56.067460Z", "iopub.status.idle": "2026-05-21T20:58:56.070671Z", "shell.execute_reply": "2026-05-21T20:58:56.070466Z", "shell.execute_reply.started": "2026-05-21T20:58:56.067516Z" } }, "outputs": [ { "data": { "text/html": [ "./
\n", "  index.rst
\n", "  tab-completion-for-modules.png
\n", "  tab-completion-for-objects.png
\n", "  tab-completion-for-anything.png
\n", "  debugging.ipynb
\n", "  magics.ipynb.license
\n", "  tab-completion-for-modules.png.license
\n", "  tab-completion-for-objects.png.license
\n", "  tab-completion-for-anything.png.license
\n", "  magics.ipynb
\n", "  shell.ipynb
\n", "  display.ipynb
\n", "  examples.ipynb.license
\n", "  examples.ipynb
\n", "  myscript.py
\n", "  importing.ipynb
\n", "  shell.ipynb.license
\n", "  extensions.rst
\n", "  start.rst
\n", "  importing.ipynb.license
\n", "  display.ipynb.license
\n", "  debugging.ipynb.license
\n", "./.ipynb_checkpoints/
\n", "  importing-checkpoint.ipynb
\n", "  display-checkpoint.ipynb
\n", "./mypackage/
\n", "  foo.ipynb.license
\n", "  __init__.py
\n", "  foo.ipynb
\n", "./mypackage/__pycache__/
\n", "  __init__.cpython-313.pyc
\n", "./unix-shell/
\n", "  index.rst
\n", "  shell-variables.ipynb.license
\n", "  create-delete.ipynb
\n", "  grep-find.ipynb.license
\n", "  create-delete.ipynb.license
\n", "  file-system.ipynb
\n", "  pipes-filters.ipynb
\n", "  shell-variables.ipynb
\n", "  pipes-filters.ipynb.license
\n", "  file-system.ipynb.license
\n", "  grep-find.ipynb
" ], "text/plain": [ "./\n", " index.rst\n", " tab-completion-for-modules.png\n", " tab-completion-for-objects.png\n", " tab-completion-for-anything.png\n", " debugging.ipynb\n", " magics.ipynb.license\n", " tab-completion-for-modules.png.license\n", " tab-completion-for-objects.png.license\n", " tab-completion-for-anything.png.license\n", " magics.ipynb\n", " shell.ipynb\n", " display.ipynb\n", " examples.ipynb.license\n", " examples.ipynb\n", " myscript.py\n", " importing.ipynb\n", " shell.ipynb.license\n", " extensions.rst\n", " start.rst\n", " importing.ipynb.license\n", " display.ipynb.license\n", " debugging.ipynb.license\n", "./.ipynb_checkpoints/\n", " importing-checkpoint.ipynb\n", " display-checkpoint.ipynb\n", "./mypackage/\n", " foo.ipynb.license\n", " __init__.py\n", " foo.ipynb\n", "./mypackage/__pycache__/\n", " __init__.cpython-313.pyc\n", "./unix-shell/\n", " index.rst\n", " shell-variables.ipynb.license\n", " create-delete.ipynb\n", " grep-find.ipynb.license\n", " create-delete.ipynb.license\n", " file-system.ipynb\n", " pipes-filters.ipynb\n", " shell-variables.ipynb\n", " pipes-filters.ipynb.license\n", " file-system.ipynb.license\n", " grep-find.ipynb" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "FileLinks(\".\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Notebooks anzeigen" ] }, { "cell_type": "code", "execution_count": 12, "metadata": { "execution": { "iopub.execute_input": "2026-05-21T20:58:56.071023Z", "iopub.status.busy": "2026-05-21T20:58:56.070964Z", "iopub.status.idle": "2026-05-21T20:58:56.134642Z", "shell.execute_reply": "2026-05-21T20:58:56.134357Z", "shell.execute_reply.started": "2026-05-21T20:58:56.071017Z" } }, "outputs": [ { "data": { "text/html": [ "\n", "\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "from pathlib import Path\n", "\n", "import nbformat\n", "\n", "from IPython.display import display as ipydisplay\n", "from pygments import highlight\n", "from pygments.formatters import HtmlFormatter\n", "from pygments.lexers import PythonLexer\n", "\n", "\n", "formatter = HtmlFormatter()\n", "lexer = PythonLexer()\n", "\n", "# publish the CSS for pygments highlighting\n", "ipydisplay(\n", " HTML(\n", " f\"\"\"\n", "\n", "\"\"\",\n", " ),\n", ")" ] }, { "cell_type": "code", "execution_count": 13, "metadata": { "execution": { "iopub.execute_input": "2026-05-21T20:58:56.135381Z", "iopub.status.busy": "2026-05-21T20:58:56.135268Z", "iopub.status.idle": "2026-05-21T20:58:56.154801Z", "shell.execute_reply": "2026-05-21T20:58:56.154532Z", "shell.execute_reply.started": "2026-05-21T20:58:56.135371Z" } }, "outputs": [ { "data": { "text/html": [ "

markdown cell

\n", "
# `foo.ipynb`
\n", "

code cell

\n", "
def bar():\n",
       "    return "bar"\n",
       "
\n", "\n", "

code cell

\n", "
def has_ip_syntax():\n",
       "    listing = !ls\n",
       "    return listing\n",
       "
\n", "\n", "

code cell

\n", "
def whatsmyname():\n",
       "    return __name__\n",
       "
\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "def show_notebook(fname):\n", " \"\"\"Display a short summary of the cells of a notebook\"\"\"\n", " nb = nbformat.read(fname, as_version=4)\n", " html = []\n", " for cell in nb.cells:\n", " html.append(f\"

{cell.cell_type} cell

\")\n", " if cell.cell_type == \"code\":\n", " html.append(highlight(cell.source, lexer, formatter))\n", " else:\n", " html.append(f\"
{cell.source}
\")\n", " ipydisplay(HTML(\"\\n\".join(html)))\n", "\n", "\n", "show_notebook(Path(\"mypackage/foo.ipynb\"))" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3.13 Kernel", "language": "python", "name": "python313" }, "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.13.0" }, "latex_envs": { "LaTeX_envs_menu_present": true, "autoclose": false, "autocomplete": true, "bibliofile": "biblio.bib", "cite_by": "apalike", "current_citInitial": 1, "eqLabelWithNumbers": true, "eqNumInitial": 1, "hotkeys": { "equation": "Ctrl-E", "itemize": "Ctrl-I" }, "labels_anchors": false, "latex_user_defs": false, "report_style_numbering": false, "user_envs_cfg": false }, "widgets": { "application/vnd.jupyter.widget-state+json": { "state": {}, "version_major": 2, "version_minor": 0 } } }, "nbformat": 4, "nbformat_minor": 4 }