Skip to content

Commit 274e39c

Browse files
Add tutorial view support (#72)
* Try something * Make tags no longer required * Remove extensions * Clean Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent 974c620 commit 274e39c

File tree

20 files changed

+123
-12
lines changed

20 files changed

+123
-12
lines changed

.actions/helpers.py

+56-3
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
import base64
2+
import json
23
import os
34
import re
4-
import shutil
55
from datetime import datetime
66
from pprint import pprint
7-
from typing import Sequence
7+
from textwrap import wrap
8+
from typing import Any, Dict, Sequence
89
from warnings import warn
910

1011
import fire
@@ -80,6 +81,12 @@
8081
# ![Pytorch Lightning](https://github.com/PyTorchLightning/pytorch-lightning/blob/master/docs/source/_static/images/logo.png){height="60px" width="240px"}
8182
8283
"""
84+
TEMPLATE_CARD_ITEM = """
85+
.. customcarditem::
86+
:header: %(title)s
87+
:card_description: %(short_description)s
88+
:tags: %(tags)s
89+
"""
8390

8491

8592
def default_requirements(path_req: str = PATH_REQ_DEFAULT) -> list:
@@ -163,6 +170,7 @@ def augment_script(fpath: str):
163170
dict(local_ipynb=f"{os.path.dirname(fpath)}.ipynb"),
164171
generated=datetime.now().isoformat(),
165172
)
173+
166174
meta['description'] = meta['description'].replace(os.linesep, f"{os.linesep}# ")
167175

168176
header = TEMPLATE_HEADER % meta
@@ -311,6 +319,43 @@ def parse_requirements(dir_path: str):
311319
with open(fname, "w") as fp:
312320
fp.write(" ".join(cmd_args))
313321

322+
@staticmethod
323+
def _get_card_item_cell(path_ipynb: str) -> Dict[str, Any]:
324+
"""Build the card item cell for the given notebook path."""
325+
fpath_meta = path_ipynb.replace(".ipynb", ".yaml")
326+
meta = yaml.safe_load(open(fpath_meta))
327+
328+
# Clamp description length
329+
wrapped_description = wrap(
330+
meta.get('short_description', meta['description']).strip().replace(os.linesep, " "), 175
331+
)
332+
suffix = "..." if len(wrapped_description) > 1 else ""
333+
meta['short_description'] = wrapped_description[0] + suffix
334+
335+
# Resolve some default tags based on accelerators and directory name
336+
meta['tags'] = meta.get('tags', [])
337+
338+
accelerators = meta.get("accelerator", ('CPU', ))
339+
if ('GPU' in accelerators) or ('TPU' in accelerators):
340+
meta['tags'].append('GPU/TPU')
341+
342+
dirname = os.path.basename(os.path.dirname(path_ipynb))
343+
if dirname != ".notebooks":
344+
meta['tags'].append(dirname)
345+
346+
meta['tags'] = ",".join(meta['tags'])
347+
348+
# Build the notebook cell
349+
rst_cell = TEMPLATE_CARD_ITEM % meta
350+
351+
return {
352+
"cell_type": "raw",
353+
"metadata": {
354+
"raw_mimetype": "text/restructuredtext"
355+
},
356+
"source": rst_cell.strip().splitlines(True)
357+
}
358+
314359
@staticmethod
315360
def copy_notebooks(path_root: str, path_docs_ipynb: str = "docs/source/notebooks"):
316361
"""Copy all notebooks from a folder to doc folder.
@@ -331,7 +376,15 @@ def copy_notebooks(path_root: str, path_docs_ipynb: str = "docs/source/notebooks
331376
new_ipynb = os.path.join(path_docs_ipynb, sub_ipynb)
332377
os.makedirs(os.path.dirname(new_ipynb), exist_ok=True)
333378
print(f'{path_ipynb} -> {new_ipynb}')
334-
shutil.copy(path_ipynb, new_ipynb)
379+
380+
with open(path_ipynb) as f:
381+
ipynb = json.load(f)
382+
383+
ipynb["cells"].append(HelperCLI._get_card_item_cell(path_ipynb))
384+
385+
with open(new_ipynb, 'w') as f:
386+
json.dump(ipynb, f)
387+
335388
ipynb_content.append(os.path.join('notebooks', sub_ipynb))
336389

337390
@staticmethod

.actions/ipynb-render.sh

+10-1
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,16 @@ python -c "import glob ; assert(len(glob.glob('$1/*.ipynb')) == 1)"
77
ipynb_file=( $(ls "$1"/*.ipynb) )
88
printf $ipynb_file
99

10+
python -c "import glob ; assert(len(glob.glob('$1/.meta.{yaml,yml}')) == 1)"
11+
meta_file=( $(ls "$1"/.meta.*) )
12+
printf $meta_file
13+
1014
pub_file=".notebooks/$1.ipynb"
1115
printf $pub_file
1216

17+
pub_meta_file=".notebooks/$1.yaml"
18+
printf $pub_meta_file
19+
1320
pub_dir=$(dirname "$pub_file")
1421
mkdir -p $pub_dir
1522

@@ -27,10 +34,12 @@ then
2734
printf "Processing: $ipynb_file\n"
2835
python -m papermill.cli $ipynb_file $pub_file --kernel python
2936
python .actions/helpers.py update-env-details $1
30-
git add ".notebooks/$1.yaml"
3137
else
3238
printf "WARNING: not valid accelerator so no outputs will be generated.\n"
3339
cp $ipynb_file $pub_file
3440
fi
3541

42+
cp $meta_file $pub_meta_file
43+
git add ".notebooks/$1.yaml"
44+
3645
git add $pub_file

.github/workflows/ci_docs.yml

+10
Original file line numberDiff line numberDiff line change
@@ -62,12 +62,14 @@ jobs:
6262
while IFS= read -r line; do
6363
bash .actions/ipynb-generate.sh $line
6464
ipynb=( $(ls "$line"/*.ipynb) )
65+
meta=( $(ls "$line"/.meta.*) )
6566
# create folder of it is missing
6667
pub_dir=$(dirname ".notebooks/$line")
6768
printf "Dir: $pub_dir"
6869
mkdir -p $pub_dir
6970
# simulate render as somple move
7071
mv $ipynb ".notebooks/$line.ipynb"
72+
mv $meta ".notebooks/$line.yaml"
7173
done <<< $(cat changed-folders.txt)
7274
shell: bash
7375

@@ -76,3 +78,11 @@ jobs:
7678
run: |
7779
# First run the same pipeline as Read-The-Docs
7880
make html --debug --jobs $(nproc) SPHINXOPTS="-W --keep-going"
81+
82+
- name: Upload built docs
83+
uses: actions/upload-artifact@v2
84+
with:
85+
name: docs-results-${{ github.sha }}
86+
path: docs/build/html/
87+
# Use always() to always run this step to publish test results when there are test failures
88+
if: success()

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -127,3 +127,5 @@ dmypy.json
127127

128128
# Pyre type checker
129129
.pyre/
130+
131+
.idea/

course_UvA-DL/autoregressive-image-modeling/.meta.yml

+2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ author: Phillip Lippe
33
created: 2021-07-12
44
updated: 2021-07-12
55
license: CC BY-SA
6+
tags:
7+
- Image
68
description: |
79
In this tutorial, we implement an autoregressive likelihood model for the task of image modeling.
810
Autoregressive models are naturally strong generative models that constitute one of the current

course_UvA-DL/deep-autoencoders/.meta.yml

+2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ author: Phillip Lippe
33
created: 2021-07-12
44
updated: 2021-07-12
55
license: CC BY-SA
6+
tags:
7+
- Image
68
description: |
79
In this tutorial, we will take a closer look at autoencoders (AE).
810
Autoencoders are trained on encoding input data such as images into a smaller feature vector,

course_UvA-DL/deep-energy-based-generative-models/.meta.yml

+2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ author: Phillip Lippe
33
created: 2021-07-12
44
updated: 2021-07-12
55
license: CC BY-SA
6+
tags:
7+
- Image
68
description: |
79
In this tutorial, we will look at energy-based deep learning models, and focus on their application as generative models.
810
Energy models have been a popular tool before the huge deep learning hype around 2012 hit.

course_UvA-DL/graph-neural-networks/.meta.yml

+2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ author: Phillip Lippe
33
created: 2021-06-07
44
updated: 2021-06-16
55
license: CC BY-SA
6+
tags:
7+
- Graph
68
description: |
79
In this tutorial, we will discuss the application of neural networks on graphs.
810
Graph Neural Networks (GNNs) have recently gained increasing popularity in both applications and research,

course_UvA-DL/normalizing-flows/.meta.yml

+2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ author: Phillip Lippe
33
created: 2021-06-07
44
updated: 2021-06-16
55
license: CC BY-SA
6+
tags:
7+
- Image
68
description: |
79
In this tutorial, we will take a closer look at complex, deep normalizing flows.
810
The most popular, current application of deep normalizing flows is to model datasets of images.

course_UvA-DL/transformers-and-MH-attention/.meta.yml

+2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ author: Phillip Lippe
33
created: 2021-06-30
44
updated: 2021-06-30
55
license: CC BY-SA
6+
tags:
7+
- Text
68
description: |
79
In this tutorial, we will discuss one of the most impactful architectures of the last 2 years: the Transformer model.
810
Since the paper Attention Is All You Need by Vaswani et al. had been published in 2017,

docs/source/conf.py

+1
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@
6666
'nbsphinx',
6767
'myst_parser',
6868
'sphinx_paramlinks',
69+
'pt_lightning_sphinx_theme.extensions.lightning_tutorials',
6970
]
7071

7172
# Add any paths that contain templates here, relative to this directory.

docs/source/index.rst

+10
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,13 @@
55
66
Lightning-Sandbox documentation
77
===============================
8+
9+
.. tutoriallist::
10+
11+
.. raw:: html
12+
13+
<div style="display:none">
14+
815
.. toctree::
916
:maxdepth: 1
1017
:name: start
@@ -14,6 +21,9 @@ Lightning-Sandbox documentation
1421
notebooks/*
1522
notebooks/**/*
1623

24+
.. raw:: html
25+
26+
</div>
1727

1828
Indices and tables
1929
==================

lightning_examples/augmentation_kornia/.meta.yml

+2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ created: 2021-06-11
44
updated: 2021-06-16
55
license: CC BY-SA
66
build: 0
7+
tags:
8+
- Image
79
description: |
810
In this tutorial we will show how to combine both Kornia.org and PyTorch Lightning
911
to perform efficient data augmentation to train a simpple model using the GPU in batch

lightning_examples/basic-gan/.meta.yaml

+2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ created: 2020-12-21
44
updated: 2021-06-16
55
license: CC BY-SA
66
build: 0
7+
tags:
8+
- Image
79
description: |
810
How to train a GAN!
911

lightning_examples/cifar10-baseline/.meta.yml

+2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ created: 2020-12-21
44
updated: 2021-06-16
55
license: CC BY-SA
66
build: 0
7+
tags:
8+
- Image
79
description: >
810
Train a Resnet to 94% accuracy on Cifar10!
911
requirements:

lightning_examples/datamodules/.meta.yml

+3-3
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ description: This notebook will walk you through how to start using Datamodules.
1010
The most up to date documentation on datamodules can be found
1111
[here](https://pytorch-lightning.readthedocs.io/en/latest/extensions/datamodules.html).
1212
requirements:
13-
- torchvision
13+
- torchvision
1414
accelerator:
15-
- CPU
16-
- GPU
15+
- CPU
16+
- GPU

lightning_examples/mnist-hello-world/.meta.yml

+5-3
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@ created: 2020-12-21
44
updated: 2021-06-16
55
license: CC BY-SA
66
build: 0
7+
tags:
8+
- Image
79
description: In this notebook, we'll go over the basics of lightning by preparing
810
models to train on the [MNIST Handwritten Digits dataset](https://en.wikipedia.org/wiki/MNIST_database).
911
requirements:
10-
- torchvision
12+
- torchvision
1113
accelerator:
12-
- CPU
13-
- GPU
14+
- CPU
15+
- GPU

lightning_examples/mnist-tpu-training/.meta.yml

+4-2
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,12 @@ author: PL team
33
created: 2020-12-21
44
updated: 2021-06-25
55
license: CC BY-SA
6+
tags:
7+
- Image
68
description: In this notebook, we'll train a model on TPUs. Updating one Trainer flag is all you need for that.
79
The most up to documentation related to TPU training can be found
810
[here](https://pytorch-lightning.readthedocs.io/en/latest/advanced/tpu.html).
911
requirements:
10-
- torchvision
12+
- torchvision
1113
accelerator:
12-
- TPU
14+
- TPU

lightning_examples/reinforce-learning-DQN/.meta.yml

+2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ created: 2021-01-31
44
updated: 2021-06-17
55
license: CC BY-SA
66
build: 0
7+
tags:
8+
- RL
79
description: |
810
Main takeaways:
911

lightning_examples/text-transformers/.meta.yml

+2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ created: 2021-01-31
44
updated: 2021-06-21
55
license: CC BY-SA
66
build: 0
7+
tags:
8+
- Text
79
description: |
810
This notebook will use HuggingFace's `datasets` library to get data, which will be wrapped in a `LightningDataModule`.
911
Then, we write a class to perform text classification on any dataset from the [GLUE Benchmark](https://gluebenchmark.com/).

0 commit comments

Comments
 (0)