From 9d006826b7784d782199f32235c9fcd04eea457b Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Robert=20J=C3=A4schke?= <jaeschke@l3s.de>
Date: Thu, 21 Jul 2022 08:27:57 +0200
Subject: [PATCH] added function to count characters in a notebook

---
 Jupyter-Demo.ipynb | 59 ++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 59 insertions(+)

diff --git a/Jupyter-Demo.ipynb b/Jupyter-Demo.ipynb
index 20ce91d..175b77c 100644
--- a/Jupyter-Demo.ipynb
+++ b/Jupyter-Demo.ipynb
@@ -25,6 +25,65 @@
     "  </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))"
+   ]
   }
  ],
  "metadata": {
-- 
GitLab