What: 5–30 mins of making JupyterLab better
When: Friday from 8am to 5:30pm
Where: 2nd floor, Gramercy
How: Walk in or sign up at: https://bit.ly/jupytercon-usertesting
Straightforward questions:
In [ ]:
import nbformat
from nbformat.v4 import new_notebook
nb = new_notebook()
display(nb)
cells
: listmetadata
: dictnbformat
, nbformat_minor
: int, intValidate
In [ ]:
nbformat.validate(nb)
What happens if it's invalid?
In [ ]:
nb.pizza = True
nbformat.validate(nb)
In [ ]:
nb = new_notebook() # get rid of pizza
from nbformat.v4 import new_code_cell, new_markdown_cell, new_raw_cell
In [ ]:
md = new_markdown_cell("First argument is the source string.")
display(md)
nb.cells.append(md)
cell_type
: str, "markdown"metadata
: dictsource
: str or list of strings
In [ ]:
raw = new_raw_cell(["Sources can also be a ","list of strings."])
display(raw)
nb.cells.append(raw)
cell_type
: str, "raw"metadata
: dictsource
: str or list of strings
In [ ]:
code = new_code_cell(["#Either way, you need newlines\n",
"print('like this')"])
display(code)
nb.cells.append(code)
cell_type
: str, "code"execution_count
: None
or intmetadata
: dictoutputs
: listsource
: str or list of stringsdisplay_data
and execute_result
can have multiple mimetypes.
For more on messages and output types:
Matthias Bussonier and Paul Ivanov's
Jupyter: Kernels, protocols, and the IPython reference implementation
In [ ]:
nbformat.write(nb, "my_demo_notebook.ipynb")
!ls my_*
In [ ]:
nb2 = nbformat.read("my_demo_notebook.ipynb", as_version=4)
print(nb2)
Andreas Mueller: Writing (and publishing) a book written in Jupyter notebooks
In [ ]:
!jupyter nbconvert my_demo_notebook.ipynb --to markdown
!cat my_demo_notebook.ipynb
Exporters specify many aspects of conversion pipeline. E.g.:
setup(…,
entry_points = {
'nbconvert.exporters':
['jekyll = my_jekyll_exporter:JekyllExporter']
}
)
Nbconvert works on single notebooks.
Multiple notebook conversion is still in early days.
Examples:
Execute for next speaker
In [ ]:
%reset -f
doit
, so Nikola is fast.
In [ ]:
from nbconvert.exporters import HTMLExporter
...
def _compile_string(self, nb_json):
"""Export notebooks as HTML strings."""
self._req_missing_ipynb()
c = Config(self.site.config['IPYNB_CONFIG'])
c.update(get_default_jupyter_config())
exportHtml = HTMLExporter(config=c)
body, _ = exportHtml.from_notebook_node(nb_json)
return body
In [ ]:
def read_metadata(self, post, lang=None):
"""Read metadata directly from ipynb file.
As ipynb files support arbitrary metadata as json, the metadata used by Nikola
will be assume to be in the 'nikola' subfield.
"""
self._req_missing_ipynb()
if lang is None:
lang = LocaleBorg().current_lang
source = post.translated_source_path(lang)
with io.open(source, "r", encoding="utf8") as in_file:
nb_json = nbformat.read(in_file, current_nbformat)
# Metadata might not exist in two-file posts or in hand-crafted
# .ipynb files.
return nb_json.get('metadata', {}).get('nikola', {})
In [ ]:
def create_post(self, path, **kw):
"""Create a new post."""
...
if content.startswith("{"):
# imported .ipynb file, guaranteed to start with "{" because it’s JSON.
nb = nbformat.reads(content, current_nbformat)
else:
nb = nbformat.v4.new_notebook()
nb["cells"] = [nbformat.v4.new_markdown_cell(content)]
In [7]:
cd /media/data/devel/damian_blog/
In [8]:
!ls
In [ ]:
title = "We are above 1000 stars!"
In [ ]:
tags_list = ['Jupyter', 'python', 'reveal', 'RISE', 'slideshow']
In [ ]:
tags = ', '.join(tags_list)
In [ ]:
!nikola new_post -f ipynb -t "{title}" --tags="{tags}"
Creating New Post
-----------------
Title: We are above 1000 stars!
Scanning posts......done!
[2017-07-12T16:45:00Z] NOTICE: compile_ipynb: No kernel specified, assuming "python3".
[2017-07-12T16:45:01Z] INFO: new_post: Your post's text is at: posts/we-are-above-1000-stars.ipynb
In [ ]:
!nikola build
In [ ]:
!nikola deploy
In [9]:
from IPython.display import IFrame
IFrame("http://www.damian.oquanta.info/", 980, 600)
Out[9]:
Execute for next speaker
In [ ]:
%reset -f
Previously, we developed a "converter" for the nbconvert library to export a ipynb to a STATIC html slideshow based in Reveal.js
library.
But with RISE, you don't have a STATIC version anymore, you have a LIVE version! A notebook rendered as a Reveal.js
-based slideshow, where you can execute code or show to the audience whatever you can show/do inside the notebook itself (but in a "slidy" way).
Slide Type
for each cell Reveal.js
understandWith RISE we provide a simple way to get a presentation view from our content with the huge amount of features coming from Reveal.js
and with all the power from the Jupyter notebook
machinery.
https://github.com/damianavila/RISE
Execute for next speaker
In [ ]:
%reset -f
A tool for creating and grading assignments in Jupyter notebooks
But challenging to use:
ClearSolutions
preprocessor)LockCells
preprocessor)ClearOutput
preprocessor)
In [ ]:
def squares(n):
"""Compute the squares of numbers from 1 to n, such that the
ith element of the returned list equals i^2.
"""
### BEGIN SOLUTION
if n < 1:
raise ValueError("n must be greater than or equal to 1")
return [i ** 2 for i in range(1, n + 1)]
### END SOLUTION
In [ ]:
def squares(n):
"""Compute the squares of numbers from 1 to n, such that the
ith element of the returned list equals i^2.
"""
# YOUR CODE HERE
raise NotImplementedError()
In [4]:
%%bash
cd course101
nbgrader assign ps1 --force --create --debug
What: 5–30 mins of making JupyterLab better
When: Friday from 8am to 5:30pm
Where: 2nd floor, Gramercy
How: Walk in or sign up at: https://bit.ly/jupytercon-usertesting