Skip to content
Snippets Groups Projects
Commit 5499e4cd authored by Axel Dürkop's avatar Axel Dürkop
Browse files

Updates

parent 3fd7e4c2
No related branches found
No related tags found
No related merge requests found
......@@ -358,7 +358,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.1"
"version": "3.6.3"
},
"toc": {
"nav_menu": {},
......
%% Cell type:markdown id: tags:
# Tipps und Tricks für die Arbeit mit HTML-Fragmenten
Wenn die HTML-Fragmente größer werden, empfehlen sich eine andere Schreibweise und Formatierung für das HTML. Aber auch der Umgang mit Platzhaltern kann optimiert werden.
%% Cell type:code id: tags:
``` python
from IPython.display import HTML
```
%% Cell type:markdown id: tags:
## HTML-Fragmente übersichtlich schreiben
Bisher sehen unsere HTML-Fragmente häufig so aus:
%% Cell type:code id: tags:
``` python
template = '<div class="meine-klasse"><a href="http://www.linkziel.de">Linkziel</a></div>'
```
%% Cell type:markdown id: tags:
Ziel sollte es sein, dass die HTML-Fragmente im Python-Kontext so notiert werden, wie sie auch in einem statischen Dokument zu finden wären. Die folgende Schreibweise macht das möglich. Auf das Syntax Highlighting des HTMLs muss leider verzichtet werden, aber - man kann nicht alles haben.
%% Cell type:code id: tags:
``` python
template = """
<div class="meine-klasse">
<a href="http://www.linkziel.de">Linkziel</a>
</div>
"""
```
%% Cell type:markdown id: tags:
## Benannte Platzhalter verwenden für `format()`
Bisher verwenden wir die Methode `format()` mit Platzhaltern, die **in der Reihenfolge ihres Vorkommens** bedient werden müssen. Das führt bei vielen Platzhaltern zu Unübersichtlichkeit. Ein besserer Ansatz ist die Arbeit mit [**benannten Platzhaltern**](https://docs.python.org/3/library/string.html#format-examples).
%% Cell type:code id: tags:
``` python
template = """
<div class="{css_class}">
<a href="{link_target}">Linkziel</a>
</div>
"""
```
%% Cell type:markdown id: tags:
Die benannten Platzhalter können durch Argumente beim Aufruf der Methode `format()` angesprochen werden. Dabei ist die Reihenfolge der Argumente beliebig.
%% Cell type:code id: tags:
``` python
html = template.format(link_target="http://heise.de", css_class="meine-klasse")
```
%% Cell type:markdown id: tags:
Noch mehr Übersichtlichkeit erreichen wir durch die listenartige Formatierung der Argumente in der Methode.
%% Cell type:code id: tags:
``` python
html = template.format(
link_target="http://heise.de",
css_class="meine-klasse"
)
```
%% Cell type:code id: tags:
``` python
HTML(html)
```
%% Output
<IPython.core.display.HTML object>
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