VSCode:如何将从 Jupyter Notebook 导入的 python 文件导出回 Jupyter 格式?
VSCode: How to export a python file that was imported from a Jupyter Notebook back to Jupyter format?
可能是个愚蠢的问题,但我找不到。 Visual Studio 代码编辑器有一个很好的方法 work with Jupyter Notebooks。我可以直接在 vscode 和 运行 中编辑单元格。现在使用版本控制会很容易。
但我找不到将其转换回笔记本的方法!如何从生成的 python 文件生成笔记本?
我知道笔记本中没有输出单元格。
交互式 Python window 中有一个选项具有笔记本输出:
这真的很酷,现在您可以在 Python 文件中工作,并且与您的版本配置系统 (Git) 有一个非常好的界面。
是的,这是一个愚蠢的问题:-)
如果您需要更多地控制如何完成到 ipynb 的转换(或者您需要 cross-references),那么您可以尝试 Pandoctools。它可以将 VSCode *.py 文档导出为任何 Pandoc 输出格式或 Jupyter notebook。
例如,您可以 create and register Jupyter kernel. For example is can be named "nn". That should be the same kernel that you selected in VSCode (there you select it by path but VSCode still uses installed kernels specs under the hood). Then add hat to the Python file, split document to cells, provide settings and set Markdown cells (commented metadata line would export to pdf instead of ipynb; I recommend to open ipynb in nteract 本机应用程序):
"""
---
kernels-map:
py: nn
jupyter:
kernelspec:
display_name: nn
language: python
name: nn
pandoctools:
# out: "*.pdf"
out: "*.ipynb"
...
# Markdown section title 1
Some **static** Markdown text.
"""
# %% {echo=False}
import IPython.display as ds
import math
import sugartex as stex
# %% {markdown}
"""
# Markdown section title 2
The quick brown Fox jumps over the lazy dog.
"""
# %%
ds.Markdown(stex.pre(f'''
Some **dynamic** Markdown text with SugarTeX formula: ˎα^˱{math.pi:1.3f}˲ˎ.
It works because of the `Markdown` display option and `sugartex` Pandoc filter.
Acually `stex.pre` is redundant here but it is needed when the text is imported
or read from somewhere instead of being written in the same document.
'''))
然后通过 pandoctools 转换文件:将文件拖放到 pandoctools shortcut/executable 或 "open with" pandoctools 可执行文件。
另见:
- 两篇介绍文章在this README、
开头
- examples 的输入到输出转换具有 cross-references!
- 如何使用 Pandoctools 以及 CLI,
- 如何使用Knitty that collects Jupyter outputs and change it's settings.
Jupytext 库支持 VSCode-Python 使用的基于百分比的单元格格式,以及其他基于文本的笔记本交换格式:
使用 jupytext 库:
安装:
pip install jupytext
现在打开 CMD 或内部终端到包含您的 .py 文件的文件夹
运行这个:
jupytext --set-formats py:percent,ipynb filename.ipynb
可能是个愚蠢的问题,但我找不到。 Visual Studio 代码编辑器有一个很好的方法 work with Jupyter Notebooks。我可以直接在 vscode 和 运行 中编辑单元格。现在使用版本控制会很容易。
但我找不到将其转换回笔记本的方法!如何从生成的 python 文件生成笔记本?
我知道笔记本中没有输出单元格。
交互式 Python window 中有一个选项具有笔记本输出:
这真的很酷,现在您可以在 Python 文件中工作,并且与您的版本配置系统 (Git) 有一个非常好的界面。
是的,这是一个愚蠢的问题:-)
如果您需要更多地控制如何完成到 ipynb 的转换(或者您需要 cross-references),那么您可以尝试 Pandoctools。它可以将 VSCode *.py 文档导出为任何 Pandoc 输出格式或 Jupyter notebook。
例如,您可以 create and register Jupyter kernel. For example is can be named "nn". That should be the same kernel that you selected in VSCode (there you select it by path but VSCode still uses installed kernels specs under the hood). Then add hat to the Python file, split document to cells, provide settings and set Markdown cells (commented metadata line would export to pdf instead of ipynb; I recommend to open ipynb in nteract 本机应用程序):
"""
---
kernels-map:
py: nn
jupyter:
kernelspec:
display_name: nn
language: python
name: nn
pandoctools:
# out: "*.pdf"
out: "*.ipynb"
...
# Markdown section title 1
Some **static** Markdown text.
"""
# %% {echo=False}
import IPython.display as ds
import math
import sugartex as stex
# %% {markdown}
"""
# Markdown section title 2
The quick brown Fox jumps over the lazy dog.
"""
# %%
ds.Markdown(stex.pre(f'''
Some **dynamic** Markdown text with SugarTeX formula: ˎα^˱{math.pi:1.3f}˲ˎ.
It works because of the `Markdown` display option and `sugartex` Pandoc filter.
Acually `stex.pre` is redundant here but it is needed when the text is imported
or read from somewhere instead of being written in the same document.
'''))
然后通过 pandoctools 转换文件:将文件拖放到 pandoctools shortcut/executable 或 "open with" pandoctools 可执行文件。
另见:
- 两篇介绍文章在this README、 开头
- examples 的输入到输出转换具有 cross-references!
- 如何使用 Pandoctools 以及 CLI,
- 如何使用Knitty that collects Jupyter outputs and change it's settings.
Jupytext 库支持 VSCode-Python 使用的基于百分比的单元格格式,以及其他基于文本的笔记本交换格式:
使用 jupytext 库:
安装:
pip install jupytext
现在打开 CMD 或内部终端到包含您的 .py 文件的文件夹
运行这个:
jupytext --set-formats py:percent,ipynb filename.ipynb