Skip to content
Snippets Groups Projects
Commit 3688bf6f authored by Jean-Luc Parouty's avatar Jean-Luc Parouty
Browse files

Update index generator for README

parent bb4e6b0d
No related branches found
No related tags found
No related merge requests found
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
import nbformat import nbformat
from nbconvert.preprocessors import ExecutePreprocessor
import re import re
import sys, os, glob import sys, os, glob
import json import json
from collections import OrderedDict from collections import OrderedDict
sys.path.append('..') sys.path.append('..')
# import fidle.pwk as pwk # import fidle.pwk as pwk
import fidle.config as config import fidle.config as config
``` ```
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
directories_to_index = ['LinearReg', 'IRIS', 'BHPD', 'MNIST', 'GTSRB', 'IMDB', 'SYNOP', 'VAE', 'Misc'] directories_to_index = ['LinearReg', 'IRIS', 'BHPD', 'MNIST', 'GTSRB', 'IMDB', 'SYNOP', 'VAE', 'Misc']
``` ```
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
def get_notebooks(directories, top_dir='..'): def get_notebooks(directories, top_dir='..'):
''' '''
Return a list of notebooks from a given list of directories Return a list of notebooks from a given list of directories
args: args:
directories : list of directories directories : list of directories
top_dir : location of theses directories top_dir : location of theses directories
return: return:
notebooks : notebooks filename list (without top_dir prefix) notebooks : notebooks filename list (without top_dir prefix)
''' '''
notebooks = [] notebooks = []
for d in directories: for d in directories:
filenames = glob.glob( f'{top_dir}/{d}/*.ipynb') filenames = glob.glob( f'{top_dir}/{d}/*.ipynb')
filenames.sort() filenames.sort()
notebooks.extend(filenames) notebooks.extend(filenames)
notebooks = [ x.replace(f'{top_dir}/','') for x in notebooks] notebooks = [ x.replace(f'{top_dir}/','') for x in notebooks]
return notebooks return notebooks
def get_infos(filename, top_dir='..'): def get_infos(filename, top_dir='..'):
''' '''
Extract informations from a fidle notebook. Extract informations from a fidle notebook.
Informations are dirname, basename, id, title, description and are extracted from comments tags in markdown. Informations are dirname, basename, id, title, description and are extracted from comments tags in markdown.
args: args:
filename : Notebook filename filename : Notebook filename
return: return:
dict : with infos. dict : with infos.
''' '''
about={} about={}
about['dirname'] = os.path.dirname(filename) about['dirname'] = os.path.dirname(filename)
about['basename'] = os.path.basename(filename) about['basename'] = os.path.basename(filename)
about['id'] = '??' about['id'] = '??'
about['title'] = '??' about['title'] = '??'
about['description'] = '??' about['description'] = '??'
# ---- Read notebook # ---- Read notebook
# #
notebook = nbformat.read(f'{top_dir}/{filename}', nbformat.NO_CONVERT) notebook = nbformat.read(f'{top_dir}/{filename}', nbformat.NO_CONVERT)
# ---- Get id, title and desc tags # ---- Get id, title and desc tags
# #
for cell in notebook.cells: for cell in notebook.cells:
if cell['cell_type'] == 'markdown': if cell['cell_type'] == 'markdown':
find = re.findall(r'<\!-- TITLE -->\s*\[(.*)\]\s*-\s*(.*)\n',cell.source) find = re.findall(r'<\!-- TITLE -->\s*\[(.*)\]\s*-\s*(.*)\n',cell.source)
if find: if find:
about['id'] = find[0][0] about['id'] = find[0][0]
about['title'] = find[0][1] about['title'] = find[0][1]
find = re.findall(r'<\!-- DESC -->\s*(.*)\n',cell.source) find = re.findall(r'<\!-- DESC -->\s*(.*)\n',cell.source)
if find: if find:
about['description'] = find[0] about['description'] = find[0]
return about return about
def get_catalog(notebooks_list, top_dir='..'): def get_catalog(notebooks_list, top_dir='..'):
''' '''
Return an OrderedDict of notebooks attributes. Return an OrderedDict of notebooks attributes.
Keys are notebooks id. Keys are notebooks id.
args: args:
notebooks_list : list of notebooks filenames notebooks_list : list of notebooks filenames
top_dir : Location of theses notebooks top_dir : Location of theses notebooks
return: return:
OrderedDict : {<notebook id> : { description} } OrderedDict : {<notebook id> : { description} }
''' '''
catalog = OrderedDict() catalog = OrderedDict()
for nb in notebooks_list: for nb in notebooks_list:
about = get_infos(nb, top_dir='..') about = get_infos(nb, top_dir='..')
id=about['id'] id=about['id']
catalog[id] = about catalog[id] = about
return catalog return catalog
``` ```
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
# ---- Get the notebook list # ---- Get the notebook list
# #
notebooks_list = get_notebooks(directories_to_index) notebooks_list = get_notebooks(directories_to_index)
# ---- Get a detailled catalog for this list # ---- Get a detailled catalog for this list
# #
catalog = get_catalog(notebooks_list) catalog = get_catalog(notebooks_list)
with open(config.CATALOG_FILE,'wt') as fp: with open(config.CATALOG_FILE,'wt') as fp:
json.dump(catalog,fp,indent=4) json.dump(catalog,fp,indent=4)
# ---- Create a markdown index # ---- Create a markdown index
# #
lines=['| | |','|--|--|'] lines=['| | |','|--|--|']
tab='&nbsp;'*5 tab='&nbsp;'*5
for id, about in catalog.items(): for id, about in catalog.items():
id = about['id'] id = about['id']
dirname = about['dirname'] dirname = about['dirname']
basename = about['basename'] basename = about['basename']
title = about['title'] title = about['title']
description = about['description'] description = about['description']
# lines.append( f'[[{id}] - {title}]({dirname}/{basename}) ' ) # lines.append( f'[[{id}] - {title}]({dirname}/{basename}) ' )
# lines.append( f'{tab}{description} ') # lines.append( f'{tab}{description} ')
lines.append( f'|{id}| [{title}]({dirname}/{basename})<br>{description}|') lines.append( f'|{id}| [{title}]({dirname}/{basename})<br>{description}|')
index = '\n'.join(lines) index = '\n'.join(lines)
# ---- Load README.md # ---- Load README.md
# #
with open('../README.md','r') as fp: with open('../README.md','r') as fp:
readme=fp.read() readme=fp.read()
# ---- Update index # ---- Update index
# #
debut = '<!-- INDEX_BEGIN -->' debut = '<!-- INDEX_BEGIN -->'
fin = '<!-- INDEX_END -->' fin = '<!-- INDEX_END -->'
readme = re.sub(f'{debut}.*{fin}',f'{debut}\n{index}\n{fin}',readme, flags=re.DOTALL) readme = re.sub(f'{debut}.*{fin}',f'{debut}\n{index}\n{fin}',readme, flags=re.DOTALL)
# ---- Update version # ---- Update version
# #
debut = '<!-- VERSION_BEGIN -->' debut = '<!-- VERSION_BEGIN -->'
fin = '<!-- VERSION_END -->' fin = '<!-- VERSION_END -->'
readme = re.sub(f'{debut}.*{fin}',f'{debut}\n{config.VERSION}\n{fin}',readme, flags=re.DOTALL) readme = re.sub(f'{debut}.*{fin}',f'{debut}\n{config.VERSION}\n{fin}',readme, flags=re.DOTALL)
# ---- Save it # ---- Save it
# #
with open('../README.md','wt') as fp: with open('../README.md','wt') as fp:
fp.write(readme) fp.write(readme)
print('README.md is updated.') print('README.md is updated.')
``` ```
%% Output %% Output
README.md is updated. README.md is updated.
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
nnb = nbformat.v4.new_notebook() # ---- Create Notebook
nbformat.v4.new_markdown_cell(source=readme) #
with open('../test.ipynb', 'w') as f: notebook = nbformat.v4.new_notebook()
nbf.write(nnb, f) new_cell = nbformat.v4.new_markdown_cell(source=readme)
``` notebook.cells.append(new_cell)
%% Output
--------------------------------------------------------------------------- # ---- Run it
NameError Traceback (most recent call last) #
<ipython-input-9-85936a69f607> in <module> ep = ExecutePreprocessor(timeout=600, kernel_name="python3")
2 nbformat.v4.new_markdown_cell(source=readme) ep.preprocess(notebook)
3 with open('../test.ipynb', 'w') as f:
----> 4 nbf.write(nnb, f)
NameError: name 'nbf' is not defined # ---- Save it
#
with open('../test.ipynb', mode="w", encoding='utf-8') as fp:
nbformat.write(notebook, fp)
```
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
``` ```
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment