使用Jupyter Notebook博客

Jupyter澳客彩票下载手机版下载 是创建“代码繁重”博客文章的绝佳环境。也许您甚至都没有打算写博客文章,但是您已经在澳客彩票下载手机版下载中进行了一些有趣的实验,然后您意识到值得分享的结果。无论哪种方式,您都需要某种方式将澳客彩票下载手机版下载放入博客。

fast_template nbdev 可以很好地处理Jupyter澳客彩票下载手机版下载。 Jupyter澳客彩票下载手机版下载和 GitHub页面 already have some support for exporting notebooks to markdown; but I suggest you use fast_template nbdev , with the process described in this post, because otherwise you won’t get useful features such as:

整个帖子都是使用这种方法创建的。你可以看到 源澳客彩票下载手机版下载在这里 .

写你的澳客彩票下载手机版下载

Your markdown cells, code cells, 和 all outputs will appear in your exported blog post. The only thing that won’t be shown is interactive outputs (e.g. ipywidgets, bokeh plots, etc), or cells you explicitly hide using the #hide marker discussed below.

当您写澳客彩票下载手机版下载时,只要写下您希望听众看到的内容即可。由于大多数写作平台都很难包含代码和输出,因此我们许多人习惯于包含比我们应有的更少的真实示例。因此,尝试养成一种新习惯,即在编写时包含许多示例。

Often you’ll want to hide boilerplate such as import statements. Add #hide to the top of any cell to make it not show up in output.

Jupyter displays the result of the last line of a cell, so there’s no need to include print(). (And including extra code that isn’t needed means there’s more cognitive overhead for the reader; so don’t include code that you don’t really need!)

1+1
2

It works not just for text, but also for images. For instance, here’s an example of the flip_lr data augmentation 使用fastai.vision :

Image.open('../fast.ai/images/fast_template/image2.png').flip_lr()

 png

…和情节:

plt.plot([1,2]);

 png

…还有Pandas DataFrames,还有更多!

pd.DataFrame({'a':[1,2], 'b':[3,4]})
a b
0 1 3
1 2 4

您还可以将图像(例如屏幕截图)直接粘贴到Jupyter中的markdown单元中。这样会在Jupyter称为“附件”的澳客彩票下载手机版下载中创建一个嵌入文件。或者,您可以使用Markdown通过URL引用Internet上的图像。

导出到降价

Before you export to markdown, you first need to make sure your notebook is saved, so press s or click FileSave. Then close the browser tab with the open notebook. This is important, because when we export to markdown any attachments will be exported to files, 和 the notebook will be updated to refer to those external files. Therefore, the notebook itself will change; if you leave it open, you may accidentally end up overwriting the updated notebook.

You’ll need nbdev installed; if you haven’t already, then install it with:

pip install  nbdev 

Then, in your terminal, cd to the folder containing your notebook, 和 type (assuming your notebook is called name.ipynb):

  nbdev  _nb2md name.ipynb

You’ll see that a file name.md 和 folder name_files have been created (where “name” is replaced by your notebook file name). One problem is that the markdown exporter assumes your images will be in name_files, but on your blog they’ll be in /images/name_files. So we need to do a search 和 replace to fix this in the markdown file. We can do this automatically with python. Create a file called upd_md.py with the following contents:

import fileinput,re
for f in fileinput.input(inplace=True):
    print(re.sub(r'^(!.*]\()(\w+_files/)', r'\1/images/\2', f), end='')

Then, in the terminal, run (assuming that upd_md.py 和 your markdown doc are in the same folder):

python upd_md.py name.md

This will modify your markdown doc inplace, so it will have the correct /images/ prefix on each image reference.

Finally, copy name_files to the images folder in your blog repo, 和 name.md to your _posts folder, 和 rename name.md to have the required YEAR-MONTH-DAY-name.md format. Commit 和 push this to GitHub, 和 you should see your post!