Skip to content
Snippets Groups Projects
Commit a7a718c8 authored by Prof. Dr. Robert Jäschke's avatar Prof. Dr. Robert Jäschke
Browse files

+stats

parent 9d006826
No related branches found
No related tags found
No related merge requests found
%% Cell type:markdown id: tags:
# Jupyter-Demo
Dieses Notebook enthält Beispiele für verschiedene Anwendungsfälle von Jupyter-Notebooks.
Lesenswerte Übersichtsseiten:
- [Komponenten von Bootstrap](https://getbootstrap.com/docs/4.5/components/) (wird von Jupyter verwendet)
- [Jupyter-Widgets](https://ipywidgets.readthedocs.io/en/latest/examples/Widget%20List.html)
## Hinweise bzw. Lösung verstecken
<details>
<summary type="button" class="btn btn-info">Hinweis / Lösung</summary>
<div class="alert alert-success" role="alert">
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).
</div>
</details>
%% Cell type:markdown id: tags:
## Widgets testen
%% Cell type:code id: tags:
```
from ipywidgets import interact
@interact(s = "*", n = (1,100))
def buchstaben(s, n):
print(s * n)
```
%% Cell type:markdown id: tags:
## Zeichen zählen
Die folgende Funktion zählt die Zeichen in den Zellen eines Notebooks:
%% Cell type:code id: tags:
```
# Quelle: https://stackoverflow.com/a/71221024
import json
def chars_markdown(fname, celltype):
"""Gibt die Anzahl Zeichen (ohne Leerzeichen) in den Zellen
vom Typ celltype im Jupyter-Notebook fname zurück."""
with open(fname) as json_file:
data = json.load(json_file)
count = 0
for cell in data['cells']:
if cell['cell_type'] == celltype:
for line in cell['source']:
count += len(line.strip().replace(" ", ""))
return count
code = chars_markdown("Jupyter-Demo.ipynb", "code")
markdown = chars_markdown("Jupyter-Demo.ipynb", "markdown")
print("Code: %6d" % code)
print("Markdown: %6d" % markdown)
print("Summe: %6d" % (code+markdown))
```
%% Cell type:markdown id: tags:
## Notebook-Statistik
Wir können das ganze noch etwas ausbauen und einige Kennzahlen zu einem Notebook ausgeben:
%% Cell type:code id: tags:
```
import json
from collections import Counter
def notebook_stats(fname):
"""Zählt Zellen, Zeilen, Wörter und Zeichen (ohne Leerzeichen) im Notebook."""
counts = {k : Counter() for k in ["markdown", "code"]}
with open(fname) as json_file:
for cell in json.load(json_file)['cells']:
c = counts[cell['cell_type']]
c["cells"] += 1
c["lines"] += len(cell['source'])
c["words"] += sum([len(line.strip().split()) for line in cell['source']])
c["chars"] += sum([len(line.strip().replace(" ", "")) for line in cell['source']])
return counts
counts = notebook_stats("Jupyter-Demo.ipynb")
print("\t", "\t".join([v for v in counts["code"].keys()]), sep='\t')
for t in ["markdown", "code"]:
print("%10s" % t, "\t".join(["%5d" % counts[t][v] for v in counts[t].keys()]), sep="\t")
```
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment