Skip to content

Commit

Permalink
Rename nb-cell/nb-raw to code-cell/raw-cell
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisjsewell committed Mar 17, 2020
1 parent fb53678 commit d37f73a
Show file tree
Hide file tree
Showing 43 changed files with 341 additions and 256 deletions.
22 changes: 11 additions & 11 deletions demo/World population.mystnb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ In the below we retrieve population data from the
[World Bank](http://www.worldbank.org/)
using the [wbdata](https://github.com/OliverSherouse/wbdata) python package

```{nb-code} ipython3
```{code-cell} ipython3
import pandas as pd
import wbdata as wb

Expand All @@ -26,15 +26,15 @@ pd.options.display.max_columns = 20
Corresponding indicator is found using search method - or, directly,
the World Bank site.

```{nb-code} ipython3
```{code-cell} ipython3
wb.search_indicators('Population, total') # SP.POP.TOTL
# wb.search_indicators('area')
# => https://data.worldbank.org/indicator is easier to use
```

Now we download the population data

```{nb-code} ipython3
```{code-cell} ipython3
indicators = {'SP.POP.TOTL': 'Population, total',
'AG.SRF.TOTL.K2': 'Surface area (sq. km)',
'AG.LND.TOTL.K2': 'Land area (sq. km)',
Expand All @@ -45,20 +45,20 @@ data

World is one of the countries

```{nb-code} ipython3
```{code-cell} ipython3
data.loc['World']
```

Can we classify over continents?

```{nb-code} ipython3
```{code-cell} ipython3
data.loc[(slice(None), '2017-01-01'), :]['Population, total'].dropna(
).sort_values().tail(60).index.get_level_values('country')
```

Extract zones manually (in order of increasing population)

```{nb-code} ipython3
```{code-cell} ipython3
zones = ['North America', 'Middle East & North Africa',
'Latin America & Caribbean', 'Europe & Central Asia',
'Sub-Saharan Africa', 'South Asia',
Expand All @@ -67,19 +67,19 @@ zones = ['North America', 'Middle East & North Africa',

And extract population information (and check total is right)

```{nb-code} ipython3
```{code-cell} ipython3
population = data.loc[zones]['Population, total'].swaplevel().unstack()
population = population[zones]
assert all(data.loc['World']['Population, total'] == population.sum(axis=1))
```

## Stacked area plot with matplotlib

```{nb-code} ipython3
```{code-cell} ipython3
import matplotlib.pyplot as plt
```

```{nb-code} ipython3
```{code-cell} ipython3
plt.clf()
plt.figure(figsize=(10, 5), dpi=100)
plt.stackplot(population.index, population.values.T / 1e9)
Expand All @@ -97,14 +97,14 @@ selected legends) are
[on their way](https://github.com/plotly/plotly.js/pull/2960) at Plotly. For
now we just do a stacked bar plot.

```{nb-code} ipython3
```{code-cell} ipython3
import plotly.offline as offline
import plotly.graph_objs as go

offline.init_notebook_mode()
```

```{nb-code} ipython3
```{code-cell} ipython3
bars = [go.Bar(x=population.index, y=population[zone], name=zone)
for zone in zones]
fig = go.Figure(data=bars,
Expand Down
6 changes: 3 additions & 3 deletions docs/formats.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ Similar to the Markdown format, MyST-NB uses code blocks to contain code cells.
The difference though, is that the metadata is contained in a YAML block:

````md
```{nb-code} ipython3
```{code-cell} ipython3
---
other:
more: true
Expand All @@ -111,7 +111,7 @@ Also, where possible the conversion will use the short-hand metadata format
(see the [MyST guide](https://myst-parser.readthedocs.io/en/latest/using/syntax.html#parameterizing-directives)):

````md
```{nb-code} ipython3
```{code-cell} ipython3
:tags: [hide-output, show-input]

print("Hallo!")
Expand All @@ -121,7 +121,7 @@ print("Hallo!")
Raw cells are also represented in a similare fashion:

````md
```{nb-raw}
```{raw-cell}
:raw_mimetype: text/html

<b>Bold text<b>
Expand Down
4 changes: 2 additions & 2 deletions jupytext/myst.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
import yaml

MYST_FORMAT_NAME = "mystnb"
CODE_DIRECTIVE = "nb-code"
RAW_DIRECTIVE = "nb-raw"
CODE_DIRECTIVE = "code-cell"
RAW_DIRECTIVE = "raw-cell"


class CompactDumper(yaml.SafeDumper):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ nbformat: 4
nbformat_minor: 2
---

```{nb-code} ipython3
```{code-cell} ipython3
1 + 1
```

A markdown cell
And below, the cell for function f has non trivial cell metadata. And the next cell as well.

```{nb-code} ipython3
```{code-cell} ipython3
---
attributes:
classes: []
Expand All @@ -25,7 +25,7 @@ def f(x):
return x
```

```{nb-code} ipython3
```{code-cell} ipython3
---
attributes:
classes: []
Expand All @@ -37,6 +37,6 @@ f(5)

More text

```{nb-code} ipython3
```{code-cell} ipython3
2 + 2
```
Original file line number Diff line number Diff line change
Expand Up @@ -7,27 +7,27 @@ nbformat: 4
nbformat_minor: 2
---

```{nb-code} ipython3
```{code-cell} ipython3
%%html
<p><a href="https://github.com/mwouts/jupytext", style="color: rgb(0,0,255)">Jupytext</a> on GitHub</p>
```

```{nb-code} ipython3
```{code-cell} ipython3
%%latex
$\frac{\pi}{2}$
```

```{nb-code} ipython3
```{code-cell} ipython3
%load_ext rpy2.ipython
```

```{nb-code} ipython3
```{code-cell} ipython3
%%R
library(ggplot2)
ggplot(data=data.frame(x=c('A', 'B'), y=c(5, 2)), aes(x,weight=y)) + geom_bar()
```

```{nb-code} ipython3
```{code-cell} ipython3
%matplotlib inline
import pandas as pd
pd.Series({'A':5, 'B':2}).plot(figsize=(3,2), kind='bar')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ This is a notebook that contains many hash signs.
Hopefully its python representation is not recognized as a Sphinx Gallery script...
##################################################################

```{nb-code} ipython3
```{code-cell} ipython3
some = 1
code = 2
some+code
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ with a code block inside it

After that cell we'll have a code cell

```{nb-code} ipython3
```{code-cell} ipython3
2 + 2


Expand All @@ -36,7 +36,7 @@ After that cell we'll have a code cell

Followed by a raw cell

```{nb-raw}
```{raw-cell}
This is
the content
of the raw cell
Expand All @@ -48,15 +48,15 @@ of the raw cell

This is a markdown cell with cell metadata `{"key": "value"}`

```{nb-code} ipython3
```{code-cell} ipython3
---
.class: null
tags: [parameters]
---
"""This is a code cell with metadata `{"tags":["parameters"], ".class":null}`"""
```

```{nb-raw}
```{raw-cell}
:key: value

This is a raw cell with cell metadata `{"key": "value"}`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,23 @@ nbformat_minor: 2

This notebook shows the use of R cells to generate plots

```{nb-code} ipython2
```{code-cell} ipython2
%load_ext rpy2.ipython
```

```{nb-code} ipython2
```{code-cell} ipython2
%%R
suppressMessages(require(tidyverse))
```

```{nb-code} ipython2
```{code-cell} ipython2
%%R
ggplot(iris, aes(x = Sepal.Length, y = Petal.Length, color=Species)) + geom_point()
```

The default plot dimensions are not good for us, so we use the -w and -h parameters in %%R magic to set the plot size

```{nb-code} ipython2
```{code-cell} ipython2
%%R -w 400 -h 240
ggplot(iris, aes(x = Sepal.Length, y = Petal.Length, color=Species)) + geom_point()
```
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ nbformat: 4
nbformat_minor: 2
---

```{nb-code} ipython3
```{code-cell} ipython3
%load_ext rpy2.ipython
import pandas as pd

Expand All @@ -21,7 +21,7 @@ df = pd.DataFrame(
)
```

```{nb-code} ipython3
```{code-cell} ipython3
%%R -i df
library("ggplot2")
ggplot(data = df) + geom_point(aes(x = X, y = Y, color = Letter, size = Z))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ nbformat_minor: 2

This notebook was created with IRKernel 0.8.12, and is not completely valid, as the code cell below contains an unexpected 'source' entry. This did cause https://github.com/mwouts/jupytext/issues/234. Note that the problem is solved when one upgrades to IRKernel 1.0.0.

```{nb-code} r
```{code-cell} r
library("ggplot2")
ggplot(mtcars, aes(mpg)) + stat_ecdf()
```
Loading

0 comments on commit d37f73a

Please sign in to comment.