将`.ipynb`文件上传到博客

发布于 2023-11-17  156 次阅读


Please refresh the page if equations are not rendered correctly.
---------------------------------------------------------------

假设已有一个包含如下内容的.ipynb文件。可以将其转换为:

  • html 代码

  • Markdowm代码

来上传到的博客。推荐第二种。

.ipynb文件

students = {


  'Student 1': {
        'Name': "Alice", 'Age' :10, 'Grade':4,
    },

    'Student 2': {
        'Name':'Bob', 'Age':11, 'Grade':5
    },

    'Student 3': {
        'Name':'Elena', 'Age':14, 'Grade':8
    }

}
students
{'Student 1': {'Name': 'Alice', 'Age': 10, 'Grade': 4},
 'Student 2': {'Name': 'Bob', 'Age': 11, 'Grade': 5},
 'Student 3': {'Name': 'Elena', 'Age': 14, 'Grade': 8}}
from google.colab import drive
drive.mount('/content/drive')
Drive already mounted at /content/drive; to attempt to forcibly remount, call drive.mount("/content/drive", force_remount=True).

Save ipynb file and then:

To Html code

!jupyter nbconvert "/content/drive/MyDrive/Colab Notebooks/ipynb2html.ipynb" --to html --template lab
[NbConvertApp] Converting notebook /content/drive/MyDrive/Colab Notebooks/ipynb2html.ipynb to html
[NbConvertApp] Writing 581495 bytes to /content/drive/MyDrive/Colab Notebooks/ipynb2html.html

Other available themes:

  • lab (The default HTML template, which produces the same DOM structure as JupyterLab)
  • classic (The HTML template styled after the classic notebook)

  • reveal (For producing slideshows).

For more details:

Creating Custom Templates for nbconvert — nbconvert 7.11.0 documentation

Finally, the html code can be send to blog editor by copy past.

This not really works well.

To markdown

!jupyter nbconvert "/content/drive/MyDrive/Colab Notebooks/ipynb2html.ipynb" --to markdown

[NbConvertApp] Converting notebook /content/drive/MyDrive/Colab Notebooks/ipynb2html.ipynb to markdown
[NbConvertApp] Writing 1780 bytes to /content/drive/MyDrive/Colab Notebooks/ipynb2html.md

届ける言葉を今は育ててる
最后更新于 2023-11-17