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>"
]
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
},
{
"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))"
]
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
},
{
"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\")"
]