-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathjupyter.cbmd
More file actions
111 lines (90 loc) · 3.45 KB
/
Copy pathjupyter.cbmd
File metadata and controls
111 lines (90 loc) · 3.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
---
title: "Codebraid with Jupyter kernels"
codebraid:
jupyter: true
---
Using Codebraid with Jupyter kernels rather than the built-in code execution
system is as simple as adding a Codebraid setting to the document YAML
metadata. For example, this document begins with the following metadata:
```
---
title: "Codebraid with Jupyter kernels"
codebraid:
jupyter: true
---
```
In this case, Codebraid automatically selects a kernel based on code language.
It is also possible to select a specific kernel. For example,
```
---
codebraid:
jupyter:
kernel: python3
---
```
This would set a default kernel for the entire document. The kernel can be
overridden for an individual session by setting `jupyter_kernel=<kernel>` on
the first code chunk of a session.
## [Matplotlib ](https://matplotlib.org/)
Plots are included automatically.
```{.python .cb-nb example=true}
%matplotlib inline
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0, 2*np.pi, 1001)
x_tick_values = np.linspace(0, 2*np.pi, 5)
x_tick_labels = ['0', r'$\pi/2$', r'$\pi$', r'$3\pi/2$', r'$2\pi$']
plt.plot(x, np.cos(x), label=r'$\cos(x)$')
plt.plot(x, np.sin(x), label=r'$\sin(x)$')
plt.xticks(x_tick_values, x_tick_labels)
plt.legend(prop={'size': 12})
plt.grid()
```
If there are errors or warnings, they are shown as well. Copying this code
into a Jupyter notebook yields the same output.
```{.python .cb-nb example=true}
plt.plot(x, np.sin(x)/x, label=r'$\sin(x)/x$')
plt.plot(x, np.cos(np.sin(x)), label=r'$\cos(\sin(x))$')
plt.xticks(x_tick_values, x_tick_labels)
plt.legend(prop={'size': 12})
plt.grid()
```
## [SymPy](https://www.sympy.org/)
SymPy equations are displayed as well. This example runs in a separate
session from the plots above. Multiple Jupyter kernels can be used within a
single document, and multiple independent sessions are possible per kernel.
```{.python .cb-nb session=sympy name=sympy1 example=true}
from sympy import *
init_printing(use_latex='mathjax')
x = Symbol('x')
eqn = E**(-x**2)
int_eqn = Integral(eqn, (x, -oo, oo))
int_eqn
```
```{.python .cb-nb session=sympy name=sympy2 example=true}
int_eqn.doit()
```
A Jupyter kernel can provide multiple formats for representing an object.
SymPy typically provides a LaTeX representation, a plain-text representation,
and a PNG representation. By default, Codebraid chooses display formats in
this order of precedence: LaTeX, Markdown, PNG, JPG, plain. (This can be
customized; see `rich_output` in the documentation for details.) So Codebraid
displays the SymPy math in LaTeX form. For this to render as nicely as
possible in the browser, Pandoc should be run with one of the flags for
rendering math in HTML, such as `--mathjax`. For this document, Pandoc was
used with `--webtex` to convert LaTeX into PNG during the document build
process.
`cb-paste` works with rich output like plots and LaTeX, just like it normally
does with code, stdout, and stderr.
```{.cb-paste copy=sympy1+sympy2 show=rich_output example=true}
```
## Customizing output
When working with `rich_output` formats that have a `text/*` mime type, such
as LaTeX and Markdown, it is possible to display the rendered output
or show the markup. For example, using `show=rich_output:latex:raw` displays
the raw (rendered) LaTeX, while `show=rich_output:latex:verbatim` displays the
LaTeX markup verbatim.
```{.cb-paste copy=sympy1 show=rich_output:latex:raw example=true}
```
```{.cb-paste copy=sympy1 show=rich_output:latex:verbatim example=true}
```