jupyter
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseJupyter Notebook Guide
Jupyter Notebook 指南
Notebooks are JSON files. Cells are in , each has (list of strings) and ('code', 'markdown', or 'raw').
nb['cells']sourcecell_typeNotebook 是JSON文件。单元格存储在中,每个单元格包含(字符串列表)和(取值为'code'、'markdown'或'raw')。
nb['cells']sourcecell_typeModifying Notebooks
修改Notebook
python
import json
with open('notebook.ipynb') as f:
nb = json.load(f)python
import json
with open('notebook.ipynb') as f:
nb = json.load(f)Modify nb['cells'][i]['source'], then:
Modify nb['cells'][i]['source'], then:
with open('notebook.ipynb', 'w') as f:
json.dump(nb, f, indent=1)
undefinedwith open('notebook.ipynb', 'w') as f:
json.dump(nb, f, indent=1)
undefinedExecuting & Converting
执行与转换
bash
jupyter nbconvert --to notebook --execute --inplace notebook.ipynb # Execute in place
jupyter nbconvert --to html notebook.ipynb # Convert to HTML
jupyter nbconvert --to script notebook.ipynb # Convert to Python
jupyter nbconvert --to markdown notebook.ipynb # Convert to Markdownbash
jupyter nbconvert --to notebook --execute --inplace notebook.ipynb # Execute in place
jupyter nbconvert --to html notebook.ipynb # Convert to HTML
jupyter nbconvert --to script notebook.ipynb # Convert to Python
jupyter nbconvert --to markdown notebook.ipynb # Convert to MarkdownFinding Code
查找代码
bash
grep -n "search_term" notebook.ipynbbash
grep -n "search_term" notebook.ipynbCell Structure
单元格结构
python
undefinedpython
undefinedCode cell
Code cell
{"cell_type": "code", "execution_count": None, "metadata": {}, "outputs": [], "source": ["code\n"]}
{"cell_type": "code", "execution_count": None, "metadata": {}, "outputs": [], "source": ["code\n"]}
Markdown cell
Markdown cell
{"cell_type": "markdown", "metadata": {}, "source": ["# Title\n"]}
undefined{"cell_type": "markdown", "metadata": {}, "source": ["# Title\n"]}
undefinedClear Outputs
清除输出
python
for cell in nb['cells']:
if cell['cell_type'] == 'code':
cell['outputs'] = []
cell['execution_count'] = Nonepython
for cell in nb['cells']:
if cell['cell_type'] == 'code':
cell['outputs'] = []
cell['execution_count'] = None