Page Menu
Home
c4science
Search
Configure Global Search
Log In
Files
F62481667
slides.py
No One
Temporary
Actions
Download File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Subscribers
None
File Metadata
Details
File Info
Storage
Attached
Created
Mon, May 13, 12:32
Size
3 KB
Mime Type
text/x-python
Expires
Wed, May 15, 12:32 (2 d)
Engine
blob
Format
Raw Data
Handle
17652056
Attached To
R3683 Slides
slides.py
View Options
#!/usr/bin/python
"""
This executable permits to generate an advanced presentation
from a Jupyter notebook.
The capabilities are:
- a server of reveal content
- a pptx (under developement)
- a beamer tex file (TODO)
"""
################################################################
import
os
import
subprocess
import
shutil
import
argparse
import
nbformat
import
count_slides
################################################################
def
mergeNotebooks
(
notebook_files
):
"Merges several notebooks in a single one named: talk.ipynb"
print
"Merging notebooks {0}"
.
format
(
notebook_files
)
notebooks
=
[
nbformat
.
read
(
f
,
as_version
=
4
)
for
f
in
notebook_files
]
cells
=
notebooks
[
0
][
'cells'
]
for
n
in
notebooks
[
1
:]:
cells
+=
n
[
'cells'
]
nbformat
.
write
(
notebooks
[
0
],
'talk.ipynb'
)
################################################################
def
launchSlideServer
(
options
=
""
):
" Launch the HTTP server for reveal slides "
reveal_path
=
'./reveal.js'
dir_name
=
os
.
path
.
dirname
(
__file__
)
slides_config_file
=
os
.
path
.
join
(
dir_name
,
'slides_config.py'
)
cmd
=
(
'jupyter nbconvert --to=slides --config {1} --post serve '
+
'--ServePostProcessor.open_in_browser=False --reveal-prefix={0} {2} '
+
'talk.ipynb'
)
.
format
(
reveal_path
,
slides_config_file
,
options
)
print
cmd
subprocess
.
call
(
cmd
,
shell
=
True
)
################################################################
def
executeNotebook
(
filename
,
options
=
""
):
" Request the execution of all cells "
print
'execute notebook: '
,
filename
dir_name
=
os
.
path
.
dirname
(
__file__
)
slides_config_file
=
os
.
path
.
join
(
dir_name
,
'slides_execute_config.py'
)
subprocess
.
call
(
'jupyter nbconvert --to=notebook --output tmp.ipynb --config'
+
' {0} {1}'
.
format
(
slides_config_file
,
filename
),
shell
=
True
)
shutil
.
move
(
'tmp.ipynb'
,
filename
)
subprocess
.
call
(
'jupyter trust {0}'
.
format
(
filename
),
shell
=
True
)
################################################################
def
getNotebookFilenames
(
slides_path
):
"""
When a directory is provided to the executable,
a file named 'slides' is searched for with
the expected list of ipython notebooks.
The list of notebooks to consider is then
returned.
"""
slides_file
=
os
.
path
.
join
(
slides_path
,
'slides'
)
if
not
os
.
path
.
isfile
(
slides_file
):
raise
Exception
(
"Cannot find slides file"
)
f
=
open
(
slides_file
)
slides
=
[
l
.
strip
()
for
l
in
f
.
readlines
()]
slides
=
[
l
for
l
in
slides
if
not
l
==
""
]
return
slides
################################################################
if
__name__
==
"__main__"
:
parser
=
argparse
.
ArgumentParser
(
description
=
'Slide launcher'
)
parser
.
add_argument
(
"--execute"
,
action
=
'store_true'
,
help
=
"Requests to execute all the notebooks launching the slides server"
)
parser
.
add_argument
(
"--no_count"
,
action
=
'store_false'
,
help
=
"Requests not to count slides generated"
)
parser
.
add_argument
(
"notebooks"
,
nargs
=
'*'
,
help
=
"The notebooks to associate in a presentation"
)
parser
.
add_argument
(
"--internet"
,
action
=
'store_true'
,
help
=
"Weather the http server should serve on the internet"
)
arguments
=
parser
.
parse_args
()
if
len
(
arguments
.
notebooks
)
==
1
:
if
os
.
path
.
isdir
(
arguments
.
notebooks
[
0
]):
arguments
.
notebooks
=
getNotebookFilenames
(
arguments
.
notebooks
[
0
])
if
arguments
.
execute
:
for
n
in
arguments
.
notebooks
:
executeNotebook
(
n
)
mergeNotebooks
(
arguments
.
notebooks
)
if
arguments
.
no_count
:
nb_slides
=
count_slides
.
countSlides
([
'talk.ipynb'
])
print
"*"
*
20
print
"You generated a talk with {0} slides"
.
format
(
nb_slides
)
print
"*"
*
20
if
arguments
.
internet
:
launchSlideServer
(
options
=
"--ServePostProcessor.ip='*'"
)
launchSlideServer
()
################################################################
Event Timeline
Log In to Comment