Skip to content
Snippets Groups Projects
Jupyter-Demo.ipynb 4.66 KiB
Newer Older
{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Jupyter-Demo\n",
    "\n",
    "Dieses Notebook enthält Beispiele für verschiedene Anwendungsfälle von Jupyter-Notebooks.\n",
    "\n",
    "Lesenswerte Übersichtsseiten:\n",
    "- [Komponenten von Bootstrap](https://getbootstrap.com/docs/4.5/components/) (wird von Jupyter verwendet)\n",
    "- [Jupyter-Widgets](https://ipywidgets.readthedocs.io/en/latest/examples/Widget%20List.html)\n",
    "\n",
    "## Hinweise bzw. Lösung verstecken\n",
    "\n",
    "<details>\n",
    "    <summary type=\"button\" class=\"btn btn-info\">Hinweis / Lösung</summary>\n",
    "  <div class=\"alert alert-success\" role=\"alert\">\n",
    "\n",
    "      \n",
    "Das `<div>`-Element ist nur notwendig, wenn eine farbliche Hervorhebung gewünscht ist (dafür sollten die [\"Alert\"-Boxen von Bootstrap](https://getbootstrap.com/docs/4.5/components/alerts/) konsistent verwendet werden). Um Markdown innerhalb von HTML-Elementen zu verwenden, muss vor und nach dem Markdown jeweils eine Zeile freigelassen werden. Die Attribute `type=\"button\" class=\"btn btn-info\"` innerhalb des `<summary>`-Tags sind [ein Trick](https://stackoverflow.com/questions/38373842/bootstrap-buttons-inside-list-group-item/38380007), um das Layout eines [Bootstrap-Buttons](https://getbootstrap.com/docs/4.5/components/buttons/) zu verwenden (andere Farben sind möglich).\n",
    "\n",
    "      \n",
    "  </div>       \n",
    "</details>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Widgets testen"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from ipywidgets import interact\n",
    "\n",
    "@interact(s = \"*\", n = (1,100))\n",
    "def buchstaben(s, n):\n",
    "    print(s * n)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Zeichen zählen\n",
    "\n",
    "Die folgende Funktion zählt die Zeichen in den Zellen eines Notebooks:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Quelle: https://stackoverflow.com/a/71221024\n",
    "import json\n",
    "\n",
    "def chars_markdown(fname, celltype):\n",
    "    \"\"\"Gibt die Anzahl Zeichen (ohne Leerzeichen) in den Zellen\n",
    "    vom Typ celltype im Jupyter-Notebook fname zurück.\"\"\"\n",
    "    with open(fname) as json_file:\n",
    "        data = json.load(json_file)\n",
    "    \n",
    "        count = 0\n",
    "        for cell in data['cells']:\n",
    "            if cell['cell_type'] == celltype:\n",
    "                for line in cell['source']:\n",
    "                    count += len(line.strip().replace(\" \", \"\"))\n",
    "        return count\n",
    "\n",
    "code = chars_markdown(\"Jupyter-Demo.ipynb\", \"code\")\n",
    "markdown = chars_markdown(\"Jupyter-Demo.ipynb\", \"markdown\")\n",
    "\n",
    "print(\"Code:     %6d\" % code)\n",
    "print(\"Markdown: %6d\" % markdown)\n",
    "print(\"Summe:    %6d\" % (code+markdown))"
   ]
Prof. Dr. Robert Jäschke's avatar
Prof. Dr. Robert Jäschke committed
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Notebook-Statistik\n",
    "\n",
    "Wir können das ganze noch etwas ausbauen und einige Kennzahlen zu einem Notebook ausgeben:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import json\n",
    "from collections import Counter\n",
    "\n",
    "\n",
    "def notebook_stats(fname):\n",
    "    \"\"\"Zählt Zellen, Zeilen, Wörter und Zeichen (ohne Leerzeichen) im Notebook.\"\"\"\n",
    "\n",
    "    counts = {k : Counter() for k in [\"markdown\", \"code\"]}\n",
    "\n",
    "    with open(fname) as json_file:\n",
    "        for cell in json.load(json_file)['cells']:\n",
    "            c = counts[cell['cell_type']]\n",
    "            c[\"cells\"] += 1\n",
    "            c[\"lines\"] += len(cell['source'])\n",
    "            c[\"words\"] += sum([len(line.strip().split()) for line in cell['source']])\n",
    "            c[\"chars\"] += sum([len(line.strip().replace(\" \", \"\")) for line in cell['source']])\n",
    "        \n",
    "    return counts\n",
    "\n",
    "\n",
    "counts = notebook_stats(\"Jupyter-Demo.ipynb\")\n",
    "\n",
    "print(\"\\t\", \"\\t\".join([v for v in counts[\"code\"].keys()]), sep='\\t')\n",
    "for t in [\"markdown\", \"code\"]:\n",
    "    print(\"%10s\" % t, \"\\t\".join([\"%5d\" % counts[t][v] for v in counts[t].keys()]), sep=\"\\t\")"
   ]
  }
 ],
 "metadata": {
  "language_info": {
   "name": "python",
   "pygments_lexer": "ipython3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}