Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
Fidle
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Deploy
Releases
Package Registry
Model registry
Operate
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Slim Karkar
Fidle
Commits
3688bf6f
Commit
3688bf6f
authored
4 years ago
by
Jean-Luc Parouty
Browse files
Options
Downloads
Patches
Plain Diff
Update index generator for README
parent
bb4e6b0d
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
fidle/Update_index.ipynb
+21
-20
21 additions, 20 deletions
fidle/Update_index.ipynb
with
21 additions
and
20 deletions
fidle/Update_index.ipynb
+
21
−
20
View file @
3688bf6f
...
@@ -7,6 +7,8 @@
...
@@ -7,6 +7,8 @@
"outputs": [],
"outputs": [],
"source": [
"source": [
"import nbformat\n",
"import nbformat\n",
"from nbconvert.preprocessors import ExecutePreprocessor\n",
"\n",
"import re\n",
"import re\n",
"import sys, os, glob\n",
"import sys, os, glob\n",
"import json\n",
"import json\n",
...
@@ -29,7 +31,7 @@
...
@@ -29,7 +31,7 @@
},
},
{
{
"cell_type": "code",
"cell_type": "code",
"execution_count":
5
,
"execution_count":
3
,
"metadata": {},
"metadata": {},
"outputs": [],
"outputs": [],
"source": [
"source": [
...
@@ -118,7 +120,7 @@
...
@@ -118,7 +120,7 @@
},
},
{
{
"cell_type": "code",
"cell_type": "code",
"execution_count":
6
,
"execution_count":
4
,
"metadata": {},
"metadata": {},
"outputs": [
"outputs": [
{
{
...
@@ -188,26 +190,25 @@
...
@@ -188,26 +190,25 @@
},
},
{
{
"cell_type": "code",
"cell_type": "code",
"execution_count":
9
,
"execution_count":
5
,
"metadata": {},
"metadata": {},
"outputs": [
"outputs": [],
{
"ename": "NameError",
"evalue": "name 'nbf' is not defined",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m<ipython-input-9-85936a69f607>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[1;32m 2\u001b[0m \u001b[0mnbformat\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mv4\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mnew_markdown_cell\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0msource\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mreadme\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 3\u001b[0m \u001b[0;32mwith\u001b[0m \u001b[0mopen\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'../test.ipynb'\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m'w'\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;32mas\u001b[0m \u001b[0mf\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 4\u001b[0;31m \u001b[0mnbf\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mwrite\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mnnb\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mf\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[0;31mNameError\u001b[0m: name 'nbf' is not defined"
]
}
],
"source": [
"source": [
"nnb = nbformat.v4.new_notebook()\n",
"# ---- Create Notebook\n",
"nbformat.v4.new_markdown_cell(source=readme)\n",
"#\n",
"with open('../test.ipynb', 'w') as f:\n",
"notebook = nbformat.v4.new_notebook()\n",
" nbf.write(nnb, f)"
"new_cell = nbformat.v4.new_markdown_cell(source=readme)\n",
"notebook.cells.append(new_cell)\n",
"\n",
"# ---- Run it\n",
"#\n",
"ep = ExecutePreprocessor(timeout=600, kernel_name=\"python3\")\n",
"ep.preprocess(notebook)\n",
"\n",
"# ---- Save it\n",
"#\n",
"with open('../test.ipynb', mode=\"w\", encoding='utf-8') as fp:\n",
" nbformat.write(notebook, fp)"
]
]
},
},
{
{
...
...
%% 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
=
'
'
*
5
tab
=
'
'
*
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
```
```
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment