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
Deploy
Releases
Container Registry
Model registry
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
Talks
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
1 merge request
!5
Update style in README
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 @@
"outputs": [],
"source": [
"import nbformat\n",
"from nbconvert.preprocessors import ExecutePreprocessor\n",
"\n",
"import re\n",
"import sys, os, glob\n",
"import json\n",
...
...
@@ -29,7 +31,7 @@
},
{
"cell_type": "code",
"execution_count":
5
,
"execution_count":
3
,
"metadata": {},
"outputs": [],
"source": [
...
...
@@ -118,7 +120,7 @@
},
{
"cell_type": "code",
"execution_count":
6
,
"execution_count":
4
,
"metadata": {},
"outputs": [
{
...
...
@@ -188,26 +190,25 @@
},
{
"cell_type": "code",
"execution_count":
9
,
"execution_count":
5
,
"metadata": {},
"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"
]
}
],
"outputs": [],
"source": [
"nnb = nbformat.v4.new_notebook()\n",
"nbformat.v4.new_markdown_cell(source=readme)\n",
"with open('../test.ipynb', 'w') as f:\n",
" nbf.write(nnb, f)"
"# ---- Create Notebook\n",
"#\n",
"notebook = nbformat.v4.new_notebook()\n",
"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:
```
python
import
nbformat
from
nbconvert.preprocessors
import
ExecutePreprocessor
import
re
import
sys
,
os
,
glob
import
json
from
collections
import
OrderedDict
sys
.
path
.
append
(
'
..
'
)
# import fidle.pwk as pwk
import
fidle.config
as
config
```
%% Cell type:code id: tags:
```
python
directories_to_index
=
[
'
LinearReg
'
,
'
IRIS
'
,
'
BHPD
'
,
'
MNIST
'
,
'
GTSRB
'
,
'
IMDB
'
,
'
SYNOP
'
,
'
VAE
'
,
'
Misc
'
]
```
%% Cell type:code id: tags:
```
python
def
get_notebooks
(
directories
,
top_dir
=
'
..
'
):
'''
Return a list of notebooks from a given list of directories
args:
directories : list of directories
top_dir : location of theses directories
return:
notebooks : notebooks filename list (without top_dir prefix)
'''
notebooks
=
[]
for
d
in
directories
:
filenames
=
glob
.
glob
(
f
'
{
top_dir
}
/
{
d
}
/*.ipynb
'
)
filenames
.
sort
()
notebooks
.
extend
(
filenames
)
notebooks
=
[
x
.
replace
(
f
'
{
top_dir
}
/
'
,
''
)
for
x
in
notebooks
]
return
notebooks
def
get_infos
(
filename
,
top_dir
=
'
..
'
):
'''
Extract informations from a fidle notebook.
Informations are dirname, basename, id, title, description and are extracted from comments tags in markdown.
args:
filename : Notebook filename
return:
dict : with infos.
'''
about
=
{}
about
[
'
dirname
'
]
=
os
.
path
.
dirname
(
filename
)
about
[
'
basename
'
]
=
os
.
path
.
basename
(
filename
)
about
[
'
id
'
]
=
'
??
'
about
[
'
title
'
]
=
'
??
'
about
[
'
description
'
]
=
'
??
'
# ---- Read notebook
#
notebook
=
nbformat
.
read
(
f
'
{
top_dir
}
/
{
filename
}
'
,
nbformat
.
NO_CONVERT
)
# ---- Get id, title and desc tags
#
for
cell
in
notebook
.
cells
:
if
cell
[
'
cell_type
'
]
==
'
markdown
'
:
find
=
re
.
findall
(
r
'
<\!-- TITLE -->\s*\[(.*)\]\s*-\s*(.*)\n
'
,
cell
.
source
)
if
find
:
about
[
'
id
'
]
=
find
[
0
][
0
]
about
[
'
title
'
]
=
find
[
0
][
1
]
find
=
re
.
findall
(
r
'
<\!-- DESC -->\s*(.*)\n
'
,
cell
.
source
)
if
find
:
about
[
'
description
'
]
=
find
[
0
]
return
about
def
get_catalog
(
notebooks_list
,
top_dir
=
'
..
'
):
'''
Return an OrderedDict of notebooks attributes.
Keys are notebooks id.
args:
notebooks_list : list of notebooks filenames
top_dir : Location of theses notebooks
return:
OrderedDict : {<notebook id> : { description} }
'''
catalog
=
OrderedDict
()
for
nb
in
notebooks_list
:
about
=
get_infos
(
nb
,
top_dir
=
'
..
'
)
id
=
about
[
'
id
'
]
catalog
[
id
]
=
about
return
catalog
```
%% Cell type:code id: tags:
```
python
# ---- Get the notebook list
#
notebooks_list
=
get_notebooks
(
directories_to_index
)
# ---- Get a detailled catalog for this list
#
catalog
=
get_catalog
(
notebooks_list
)
with
open
(
config
.
CATALOG_FILE
,
'
wt
'
)
as
fp
:
json
.
dump
(
catalog
,
fp
,
indent
=
4
)
# ---- Create a markdown index
#
lines
=
[
'
| | |
'
,
'
|--|--|
'
]
tab
=
'
'
*
5
for
id
,
about
in
catalog
.
items
():
id
=
about
[
'
id
'
]
dirname
=
about
[
'
dirname
'
]
basename
=
about
[
'
basename
'
]
title
=
about
[
'
title
'
]
description
=
about
[
'
description
'
]
# lines.append( f'[[{id}] - {title}]({dirname}/{basename}) ' )
# lines.append( f'{tab}{description} ')
lines
.
append
(
f
'
|
{
id
}
| [
{
title
}
](
{
dirname
}
/
{
basename
}
)<br>
{
description
}
|
'
)
index
=
'
\n
'
.
join
(
lines
)
# ---- Load README.md
#
with
open
(
'
../README.md
'
,
'
r
'
)
as
fp
:
readme
=
fp
.
read
()
# ---- Update index
#
debut
=
'
<!-- INDEX_BEGIN -->
'
fin
=
'
<!-- INDEX_END -->
'
readme
=
re
.
sub
(
f
'
{
debut
}
.*
{
fin
}
'
,
f
'
{
debut
}
\n
{
index
}
\n
{
fin
}
'
,
readme
,
flags
=
re
.
DOTALL
)
# ---- Update version
#
debut
=
'
<!-- VERSION_BEGIN -->
'
fin
=
'
<!-- VERSION_END -->
'
readme
=
re
.
sub
(
f
'
{
debut
}
.*
{
fin
}
'
,
f
'
{
debut
}
\n
{
config
.
VERSION
}
\n
{
fin
}
'
,
readme
,
flags
=
re
.
DOTALL
)
# ---- Save it
#
with
open
(
'
../README.md
'
,
'
wt
'
)
as
fp
:
fp
.
write
(
readme
)
print
(
'
README.md is updated.
'
)
```
%% Output
README.md is updated.
%% Cell type:code id: tags:
```
python
nnb
=
nbformat
.
v4
.
new_notebook
()
nbformat
.
v4
.
new_markdown_cell
(
source
=
readme
)
with
open
(
'
../test.ipynb
'
,
'
w
'
)
as
f
:
nbf
.
write
(
nnb
,
f
)
```
%% Output
# ---- Create Notebook
#
notebook
=
nbformat
.
v4
.
new_notebook
()
new_cell
=
nbformat
.
v4
.
new_markdown_cell
(
source
=
readme
)
notebook
.
cells
.
append
(
new_cell
)
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
<ipython-input-9-85936a69f607> in <module>
2 nbformat.v4.new_markdown_cell(source=readme)
3 with open('../test.ipynb', 'w') as f:
----> 4 nbf.write(nnb, f)
# ---- Run it
#
ep
=
ExecutePreprocessor
(
timeout
=
600
,
kernel_name
=
"
python3
"
)
ep
.
preprocess
(
notebook
)
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:
```
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