Skip to content

Commit 07e2736

Browse files
committed
Add option to generate Try QuTiP URLs.
1 parent bdcc406 commit 07e2736

File tree

3 files changed

+54
-31
lines changed

3 files changed

+54
-31
lines changed

website/create_index.py

Lines changed: 38 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import os
55
import pathlib
66
import re
7+
import urllib.parse
78

89
from jinja2 import (
910
Environment,
@@ -36,16 +37,25 @@ class Notebook:
3637
""" Notebook object for use in rendering templates. """
3738

3839
NBVIEWER_URL_PREFIX = "https://nbviewer.org/urls/qutip.org/qutip-tutorials/"
40+
TRY_QUTIP_URL_PREFIX = "https://qutip.org/try-qutip/lab/index.html?"
3941

40-
def __init__(self, path, title):
41-
tutorial_folder = path.parent.parent
42-
web_folder = tutorial_folder.parent
42+
def __init__(self, title, tutorial_folder, path):
43+
self.tutorial_folder = tutorial_folder
44+
self.web_folder = tutorial_folder.parent
4345

44-
self.web_md_path = path.relative_to(web_folder)
45-
self.web_ipynb_path = self.web_md_path.with_suffix(".ipynb")
4646
self.title = title
4747

48-
self.url = self.NBVIEWER_URL_PREFIX + self.web_ipynb_path.as_posix()
48+
self.web_md_path = path.relative_to(self.web_folder)
49+
self.web_ipynb_path = self.web_md_path.with_suffix(".ipynb")
50+
51+
self.tutorial_md_path = path.relative_to(self.tutorial_folder)
52+
self.tutorial_ipynb_path = self.tutorial_md_path.with_suffix(".ipynb")
53+
54+
self.nbviewer_url = self.NBVIEWER_URL_PREFIX + self.web_ipynb_path.as_posix()
55+
self.try_qutip_url = (
56+
self.TRY_QUTIP_URL_PREFIX +
57+
urllib.parse.urlencode({"path": "tutorials/" + self.tutorial_ipynb_path.as_posix()})
58+
)
4959

5060

5161
def get_title(path):
@@ -82,7 +92,10 @@ def get_notebooks(tutorials_folder, subfolder):
8292
files = list((tutorials_folder / subfolder).glob("*.md"))
8393
titles = [get_title(f) for f in files]
8494
files_sorted, titles_sorted = sort_files_titles(files, titles)
85-
notebooks = [Notebook(f, t) for f, t in zip(files_sorted, titles_sorted)]
95+
notebooks = [
96+
Notebook(title, tutorials_folder, path)
97+
for title, path in zip(titles_sorted, files_sorted)
98+
]
8699
return notebooks
87100

88101

@@ -106,18 +119,28 @@ def render_template(template_path, **kw):
106119

107120
def parse_args():
108121
parser = argparse.ArgumentParser(
109-
description="Generate indexes for tutorial notebooks.",
110-
)
111-
parser.add_argument(
112-
"index_type", choices=["html", "notebook"],
113-
metavar="INDEX_TYPE",
114-
help="Whether to generate an HTML or Markdown Jupyter notebook index [html, notebook].",
122+
description="""
123+
Generate indexes for tutorial notebooks.
124+
125+
This script is used both by this repository to generate the indexes
126+
for the QuTiP tutorial website and by https://github.com/qutip/try-qutip/
127+
to generate the notebook indexes for the Try QuTiP site.
128+
""",
115129
)
116130
parser.add_argument(
117131
"qutip_version", choices=["v4", "v5"],
118132
metavar="QUTIP_VERSION",
119133
help="Which QuTiP version to generate the tutorial index for [v4, v5].",
120134
)
135+
parser.add_argument(
136+
"index_type", choices=["html", "try-qutip"],
137+
metavar="INDEX_TYPE",
138+
help=(
139+
"Whether to generate an HTML index for the website or"
140+
" a Markdown Jupyter notebook index for the Try QuTiP site"
141+
" [html, try-qutip]."
142+
),
143+
)
121144
parser.add_argument(
122145
"output_file",
123146
metavar="OUTPUT_FILE",
@@ -160,8 +183,8 @@ def main():
160183
version_note=version_note,
161184
tutorials=tutorials,
162185
)
163-
elif args.index_type == "notebook":
164-
template = root_folder / "website" / "index.notebook.jinja"
186+
elif args.index_type == "try-qutip":
187+
template = root_folder / "website" / "index.try-qutip.jinja"
165188
text = render_template(
166189
template,
167190
title=title,

website/index.html.jinja

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ useful to have a look at these IPython notebook
6868
<h4 id="python-introduction">Python Introduction</h4>
6969
<ul>
7070
{% for item in tutorials['python-introduction'] %}
71-
<li><a href="{{ item.url }}">{{ item.title }}</a></li>
71+
<li><a href="{{ item.nbviewer_url }}">{{ item.title }}</a></li>
7272
{% endfor %}
7373
</ul>
7474
<p>For a more in depth discussion see: <a href="https://github.com/jrjohansson/scientific-python-lectures">Lectures on scientific computing with Python</a>.</p>
@@ -77,7 +77,7 @@ useful to have a look at these IPython notebook
7777
<h4 id="visualizations">Visualization</h4>
7878
<ul>
7979
{% for item in tutorials['visualization'] %}
80-
<li><a href="{{ item.url }}">{{ item.title }}</a></li>
80+
<li><a href="{{ item.nbviewer_url }}">{{ item.title }}</a></li>
8181
{% endfor %}
8282
</ul>
8383

@@ -87,14 +87,14 @@ useful to have a look at these IPython notebook
8787
<h5 id="qip-circuits">Quantum circuits and algorithms</h5>
8888
<ul>
8989
{% for item in tutorials['quantum-circuits'] %}
90-
<li><a href="{{ item.url }}">{{ item.title }}</a></li>
90+
<li><a href="{{ item.nbviewer_url }}">{{ item.title }}</a></li>
9191
{% endfor %}
9292
</ul>
9393

9494
<h5 id="qip-pulse-level">Pulse-level circuit simulation</h5>
9595
<ul>
9696
{% for item in tutorials['pulse-level-circuit-simulation'] %}
97-
<li><a href="{{ item.url }}">{{ item.title }}</a></li>
97+
<li><a href="{{ item.nbviewer_url }}">{{ item.title }}</a></li>
9898
{% endfor %}
9999
</ul>
100100

@@ -105,7 +105,7 @@ useful to have a look at these IPython notebook
105105
<h4 id="time-evolution">Time evolution</h4>
106106
<ul>
107107
{% for item in tutorials['time-evolution'] %}
108-
<li><a href="{{ item.url }}">{{ item.title }}</a></li>
108+
<li><a href="{{ item.nbviewer_url }}">{{ item.title }}</a></li>
109109
{% endfor %}
110110
</ul>
111111

@@ -145,14 +145,14 @@ useful to have a look at these IPython notebook
145145
<h4 id="heom">Hierarchical Equations of Motion</h4>
146146
<ul>
147147
{% for item in tutorials['heom'] %}
148-
<li><a href="{{ item.url }}">{{ item.title }}</a></li>
148+
<li><a href="{{ item.nbviewer_url }}">{{ item.title }}</a></li>
149149
{% endfor %}
150150
</ul>
151151

152152
<h4 id="miscellaneous">Miscellaneous tutorials</h4>
153153
<ul>
154154
{% for item in tutorials['miscellaneous'] %}
155-
<li><a href="{{ item.url }}">{{ item.title }}</a></li>
155+
<li><a href="{{ item.nbviewer_url }}">{{ item.title }}</a></li>
156156
{% endfor %}
157157
</ul>
158158

@@ -168,7 +168,7 @@ topics and analyze them numerically using QuTiP (some more detailed than others)
168168

169169
<ul>
170170
{% for item in tutorials['lectures'] %}
171-
<li><a href="{{ item.url }}">{{ item.title }}</a></li>
171+
<li><a href="{{ item.nbviewer_url }}">{{ item.title }}</a></li>
172172
{% endfor %}
173173
</ul>
174174

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,14 @@ jupyter:
1818
## Python Introduction
1919

2020
{% for item in tutorials['python-introduction'] %}
21-
- [{{ item.title }}]({{ item.url }})
21+
- [{{ item.title }}]({{ item.try_qutip_url }})
2222
{% endfor %}
2323

2424

2525
## Visualization
2626

2727
{% for item in tutorials['visualization'] %}
28-
- [{{ item.title }}]({{ item.url }})
28+
- [{{ item.title }}]({{ item.try_qutip_url }})
2929
{% endfor %}
3030

3131

@@ -36,34 +36,34 @@ This section requires an additional package [qutip-qip](https://github.com/qutip
3636
### Quantum circuits and algorithms
3737

3838
{% for item in tutorials['quantum-circuits'] %}
39-
- [{{ item.title }}]({{ item.url }})
39+
- [{{ item.title }}]({{ item.try_qutip_url }})
4040
{% endfor %}
4141

4242
### Pulse-level circuit simulation
4343

4444
{% for item in tutorials['pulse-level-circuit-simulation'] %}
45-
- [{{ item.title }}]({{ item.url }})
45+
- [{{ item.title }}]({{ item.try_qutip_url }})
4646
{% endfor %}
4747

4848

4949
## Time evolution
5050

5151
{% for item in tutorials['time-evolution'] %}
52-
- [{{ item.title }}]({{ item.url }})
52+
- [{{ item.title }}]({{ item.try_qutip_url }})
5353
{% endfor %}
5454

5555

5656
## Hierarchical Equations of Motion
5757

5858
{% for item in tutorials['heom'] %}
59-
- [{{ item.title }}]({{ item.url }})
59+
- [{{ item.title }}]({{ item.try_qutip_url }})
6060
{% endfor %}
6161

6262

6363
## Miscellaneous tutorials
6464

6565
{% for item in tutorials['miscellaneous'] %}
66-
- [{{ item.title }}]({{ item.url }})
66+
- [{{ item.title }}]({{ item.try_qutip_url }})
6767
{% endfor %}
6868

6969

@@ -73,7 +73,7 @@ These lecture-style notebooks focus on particular quantum mechanics
7373
topics and analyze them numerically using QuTiP (some more detailed than others).
7474

7575
{% for item in tutorials['lectures'] %}
76-
- [{{ item.title }}]({{ item.url }})
76+
- [{{ item.title }}]({{ item.try_qutip_url }})
7777
{% endfor %}
7878

7979

0 commit comments

Comments
 (0)