44import os
55import pathlib
66import re
7+ import urllib .parse
78
89from 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
5161def 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
107120def 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 ,
0 commit comments