Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
jupyter-notebooks
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Deploy
Releases
Container Registry
Model registry
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
ITBH
itbh-inf-wise201718
jupyter-notebooks
Commits
f313b54c
Commit
f313b54c
authored
7 years ago
by
Axel Dürkop
Browse files
Options
Downloads
Patches
Plain Diff
Upload New File
parent
9bcdfeb3
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
Navigation mit Python.ipynb
+200
-0
200 additions, 0 deletions
Navigation mit Python.ipynb
with
200 additions
and
0 deletions
Navigation mit Python.ipynb
0 → 100644
+
200
−
0
View file @
f313b54c
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Dynamisches Generieren einer Navigation mit Python"
]
},
{
"cell_type": "code",
"execution_count": 31,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"<nav><ul style=\"list-style: none;\"><li><a href=\"#\">Home</a></li><li><a href=\"#\">Kontakt</a></li><li><a href=\"#\">Impressum</a></li></ul></nav>\n"
]
}
],
"source": [
"navi = [\"Home\", \"Kontakt\", \"Impressum\"]\n",
"\n",
"nav_wrapper = '<nav><ul style=\"list-style: none;\">{}</ul></nav>'\n",
"nav_item_wrapper = '<li><a href=\"#\">{}</a></li>'\n",
"\n",
"nav_items = \"\"\n",
"\n",
"for n in navi:\n",
" nav_items = nav_items + nav_item_wrapper.format(n)\n",
" \n",
"html_navigation = nav_wrapper.format(nav_items)\n",
"\n",
"print(html_navigation)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Ein Trick für's Notebook"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Um das generierte HTML aus \"als\" HTML ausgeben zu können, müssen wir uns hier im Notebook eines Trick bedienen."
]
},
{
"cell_type": "code",
"execution_count": 30,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<nav>\n",
"<ul style=\"list-style: none;\">\n",
"<li><a href=\"#\">Home</a></li>\n",
"<li><a href=\"#\">Kontakt</a></li>\n",
"<li><a href=\"#\">Impressum</a></li>\n",
"</ul></nav>"
],
"text/plain": [
"<IPython.core.display.HTML object>"
]
},
"execution_count": 30,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"from IPython.display import HTML\n",
"\n",
"HTML(html_navigation)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Keine Liste, sondern Dicationary"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Statt einer Liste verwenden wir nun ein *dictionary*:"
]
},
{
"cell_type": "code",
"execution_count": 29,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"http://spiegel.de\n"
]
}
],
"source": [
"navi = {\n",
" \"home\" : {\n",
" \"name\": \"Home\",\n",
" \"url\": \"http://heise.de\",\n",
" \"external\": True\n",
" },\n",
" \"kontakt\": {\n",
" \"name\": \"Kontakt\",\n",
" \"url\": \"http://spiegel.de\",\n",
" \"external\": False\n",
" }\n",
"}\n",
"\n",
"print(navi[\"kontakt\"][\"url\"])"
]
},
{
"cell_type": "code",
"execution_count": 30,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"<nav><ul style=\"list-style: none;\"><li><a href=\"http://spiegel.de\">Kontakt</a></li><li><a href=\"http://heise.de\">Home</a></li></ul></nav>\n"
]
}
],
"source": [
"nav_wrapper = '<nav><ul style=\"list-style: none;\">{}</ul></nav>'\n",
"nav_item_wrapper = '<li><a href=\"{}\">{}</a></li>'\n",
"\n",
"nav_items = \"\"\n",
"\n",
"for key, value in navi.items():\n",
" nav_items = nav_items + nav_item_wrapper.format(value[\"url\"],value['name'])\n",
"html_navigation = nav_wrapper.format(nav_items)\n",
"\n",
"print(html_navigation)"
]
},
{
"cell_type": "code",
"execution_count": 28,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<nav><ul style=\"list-style: none;\"><li><a href=\"#\">Kontakt</a></li><li><a href=\"#\">Home</a></li></ul></nav>"
],
"text/plain": [
"<IPython.core.display.HTML object>"
]
},
"execution_count": 28,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"from IPython.display import HTML\n",
"\n",
"HTML(html_navigation)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.5.4"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
%% Cell type:markdown id: tags:
# Dynamisches Generieren einer Navigation mit Python
%% Cell type:code id: tags:
```
python
navi
=
[
"
Home
"
,
"
Kontakt
"
,
"
Impressum
"
]
nav_wrapper
=
'
<nav><ul style=
"
list-style: none;
"
>{}</ul></nav>
'
nav_item_wrapper
=
'
<li><a href=
"
#
"
>{}</a></li>
'
nav_items
=
""
for
n
in
navi
:
nav_items
=
nav_items
+
nav_item_wrapper
.
format
(
n
)
html_navigation
=
nav_wrapper
.
format
(
nav_items
)
print
(
html_navigation
)
```
%% Output
<nav><ul style="list-style: none;"><li><a href="#">Home</a></li><li><a href="#">Kontakt</a></li><li><a href="#">Impressum</a></li></ul></nav>
%% Cell type:markdown id: tags:
## Ein Trick für's Notebook
%% Cell type:markdown id: tags:
Um das generierte HTML aus "als" HTML ausgeben zu können, müssen wir uns hier im Notebook eines Trick bedienen.
%% Cell type:code id: tags:
```
python
from
IPython.display
import
HTML
HTML
(
html_navigation
)
```
%% Output
<IPython.core.display.HTML object>
%% Cell type:markdown id: tags:
## Keine Liste, sondern Dicationary
%% Cell type:markdown id: tags:
Statt einer Liste verwenden wir nun ein
*dictionary*
:
%% Cell type:code id: tags:
```
python
navi
=
{
"
home
"
:
{
"
name
"
:
"
Home
"
,
"
url
"
:
"
http://heise.de
"
,
"
external
"
:
True
},
"
kontakt
"
:
{
"
name
"
:
"
Kontakt
"
,
"
url
"
:
"
http://spiegel.de
"
,
"
external
"
:
False
}
}
print
(
navi
[
"
kontakt
"
][
"
url
"
])
```
%% Output
http://spiegel.de
%% Cell type:code id: tags:
```
python
nav_wrapper
=
'
<nav><ul style=
"
list-style: none;
"
>{}</ul></nav>
'
nav_item_wrapper
=
'
<li><a href=
"
{}
"
>{}</a></li>
'
nav_items
=
""
for
key
,
value
in
navi
.
items
():
nav_items
=
nav_items
+
nav_item_wrapper
.
format
(
value
[
"
url
"
],
value
[
'
name
'
])
html_navigation
=
nav_wrapper
.
format
(
nav_items
)
print
(
html_navigation
)
```
%% Output
<nav><ul style="list-style: none;"><li><a href="http://spiegel.de">Kontakt</a></li><li><a href="http://heise.de">Home</a></li></ul></nav>
%% Cell type:code id: tags:
```
python
from
IPython.display
import
HTML
HTML
(
html_navigation
)
```
%% Output
<IPython.core.display.HTML object>
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment