2.7. Group Code Blocks into Tabs

Here is an example showing grouping code blocks into three tabs.

2.7.1. Example

Let’s implement \(a+b\). We first show instructions, then demonstrate the codes.

You need to have python installed

a = [1,1,1]
b = [2,2,2]
[ia+ib for ia, ib in zip(a,b)]
[3, 3, 3]

You can install numpy by

pip install numpy
import numpy as np
a = np.ones(3)
b = np.ones(3)*2
a + b
array([3., 3., 3.])

Please install cpython

# Just a place holder
print(1+2)
3

Next let’s implement \(a - b\)

a = [1,1,1]
b = [2,2,2]
[ia-ib for ia, ib in zip(a,b)]
[-1, -1, -1]
a = np.ones(3)
b = np.ones(3)*2
a - b
array([-1., -1., -1.])

2.7.2. Usages

To enable multi-tabs, first configure the tabs entry in the config.ini file. For example, here we use tabs = python, numpy, cpython. python is the default tab. To specify a code block that doesn’t belong to the default tab, add #@tab, followed by the tab name (case insensitive), in the first line of the code block.

Sometimes these codes blocks conflict with each others. We can activate one tab at a time, so only code blocks belong to this tab can be evaluated in Jupyter. For example

d2lbook activate default user/code_tabs.md  # activate the default tab
d2lbook activate numpy user/code_tabs.md    # activate the numpy tab
d2lbook activate all user/code_tabs.md      # activate all tabs