Allows display/dowload of python scripts as text files
@@ -110,18 +110,17 @@ def modif_nb_links(folder, url_docs):
DOWNLOAD_CELL = """
<div class='admonition note'>
-This is a *fixed-text* formatted version of a Jupyter notebook.
+**This is a fixed-text formatted version of a Jupyter notebook.**
You can download for each version of *gammapy* a
[HTMLZip pack](http://readthedocs.org/projects/gammapy/downloads/) containing
-the whole documentation and full collection of notebooks, so you can execute
-them in your local _static/notebooks/ folder. You can also contribute with
-your own notebooks in this
-[GitHub repository](https://github.com/gammapy/gammapy-extra/tree/master/notebooks).
+the whole HTML documentation and full collection of notebooks, so you can execute
+them in your local `_static/notebooks/` folder. You can also contribute with your
+own notebooks in this [GitHub repository](https://github.com/gammapy/gammapy-extra/tree/master/notebooks).
-**Download source files:**
+**Source files:**
[{nb_filename}](../_static/notebooks/{nb_filename}) |
-[{py_filename}](../_static/notebooks/{py_filename})
+[{py_filename}](../_static/notebooks/{txt_filename})
</div>"""
for filename in os.listdir(folder):
@@ -129,7 +128,8 @@ your own notebooks in this
if os.path.isfile(filepath) and filepath[-6:] == '.ipynb':
if folder=='notebooks':
py_filename = filename.replace('ipynb', 'py')
- ctx = dict(nb_filename=filename, py_filename=py_filename)
+ txt_filename = filename.replace('ipynb', 'txt')
+ ctx = dict(nb_filename=filename, py_filename=py_filename, txt_filename=txt_filename)
strcell = DOWNLOAD_CELL.format(**ctx)
nb = nbformat.read(filepath, as_version=nbformat.NO_CONVERT)
nb.cells.insert(0, new_markdown_cell(strcell))
@@ -143,6 +143,15 @@ your own notebooks in this
with open(filepath, "w") as f:
f.write(txt)
+def replace_extension(folder):
+ """
+ Replaces extension of .py files so they can be served for download
+ """
+
+ for filename in os.listdir(folder):
+ txtfilename = filename.replace('.py', '.txt')
+ os.rename(os.path.join(folder, filename), os.path.join(folder, txtfilename))
+
def gammapy_sphinx_notebooks(setup_cfg):
"""
Manages the processes for the building of sphinx formatted notebooks
@@ -166,5 +175,6 @@ def gammapy_sphinx_notebooks(setup_cfg):
copytree(gammapy_extra_notebooks_folder, 'notebooks', ignore=ignorefiles)
copytree(gammapy_extra_notebooks_folder, '_static/notebooks')
os.system('jupyter nbconvert --to script _static/notebooks/*.ipynb')
+ replace_extension('_static/notebooks')
modif_nb_links('notebooks', url_docs)
modif_nb_links('_static/notebooks', url_docs)
