数据科学与Python国外Python教程Python开发者

打造数据科学作品集:搭建一个数据科学博客

2016-10-10  本文已影响296人  EarlGrey

这是「打造数据科学的作品集」系列文章的第二篇。如果你喜欢该系列,而且想知道本系列的下一篇文章什么时候发布,你可以订阅我们。读完本文,你将学会如何使用 Pelican 静态网站生成器,搭建一个属于自己的博客,用来展示数据科学作品。

全文大约 9500 字,读完需要 15 分钟左右。

作者:Vik Paruchuri@Dataquest.io
译者:cystone
校对:EarlGrey
出品:PythonTG 翻译组/编程派

你可以在这里阅读本系列第一篇文章:「打造数据科学作品集:用数据讲故事」

写博客是证明你的实力、深入学习和建立读者群的好方法。有许多数据科学编程类博客帮助他们的作者找到工作,或者认识了重要人物。定期写博客是有抱负的程序员和数据科学家最应该做的事情之一。

不幸的是,写博客的一大障碍就是先搭建一个博客网站。在这篇文章中,我们将学习如何用 Python 创建一个博客网站,怎么用 Jupyter Notebook 写文章和如何通过 GitHub Pages 部署博客。读完这篇文章,你就可以使用你熟悉的方式,创建自己的数据科学博客了。

静态网站

基本上,一个静态网站就是一个全是 HTML 文件的文件夹。我们可以搭建一个允许别人链接到这个文件夹并获取文件的服务器。这样做的好处是不需要数据库或者其他动态部分,可以很简单的部署在像 GitHub 之类的网站上。把你的博客做成静态网站是一个好主意,因为维护起来十分简单。建立静态网站的一种方法是手写 HTML,然后上传所有的 HTML 文件到服务器。这种情况下,你至少要写一个 index.html 文件。如果你的网站的 URL 是 thebestblog.com,当访问者浏览 http://www.thebestblog.com 时,他们就会看到 index.html 的内容了。HTML 的文件夹可能是下边的这个样子:

thebestblog.com
│   index.html
│   first-post.html
│   how-to-use-python.html
│   how-to-do-machine-learning.html
│   styles.css

在上边的这个网站里,访问 http://www.thebestblog.com/first-post.html 你就可以看到first-post.html 的内容。first-post.html 可能是下边这个样子:

<html>
<head>
  <title>The best blog!</title>
  <meta name="description" content="The best blog!"/>
  <link rel="stylesheet" href="styles.css" />
</head>
<body>
  <h1>First post!</h1>
  <p>This is the first post in what will soon become (if it already isn't) the best blog.</p>
  <p>Future posts will teach you about data science.</p>

<div class="footer">
  <p>Thanks for visiting!</p>
</div>
</body>
</html>

你可能会立马发现手工编辑 HTML 的一些问题:

一般来说,你写博客的时候,想关注于博客内容,而不是在 HTML 上浪费时间。谢天谢地,你可以用一个叫做静态网站生成器的工具来取代手动编辑 HTML。

静态网站生成器

静态网站生成器可以让你用一些简单的格式写文章,通常是 Markdown,然后再定义一些设置。生成器可以自动把你的文章转换为 HTMl。使用静态网站生成器,你可以把 first-post.html 极大地简化为 first-post.md

# First post!

This is the first post in what will soon become (if it already isn't) the best blog.

Future posts will teach you about data science.

这比处理 HTML 文件要简单的多!通用的元素,比如 Title 和 Footer,可以放在模板里边,这样很容易更改。

静态网站生成器多种多样。最流行的是用 Ruby 开发的 Jekyll。因为我们要搭建一个数据科学博客,所以需要网站生成器可以处理 Jupyter Notebooks。

Pelican 是一个用 Python 开发的网站生成器,可以接受 Jupyter Notebook 文件并转换成 HTML 博客文章。Pelican 也可以很容易的把文章部署到 GitHub Pages 让别人阅读。

安装 Pelican

开始之前,这里有一个仓库(repo),它就是我们最终成果的示例。

如果你还没有安装 Python,在开始之前你还需要做一些前期工作。这里有一些安装 Python 的说明。我们建议使用 Python3.5。当你安装完成 Python:

Markdown==2.6.6
pelican==3.6.3
jupyter>=1.0
ipython>=4.0
nbconvert>=4.0
beautifulsoup4
ghp-import==0.4.1
matplotlib==1.5.1

创建数据科学博客

完成了前边的设置之后,你就做完创建博客的准备了!在 jupyter-blog 文件夹里运行 pelican-quickstart 命令,来为你的博客启动一个交互式安装序列。你将看到一些帮助你设置博客属性的问题。大多数问题你只需要点击 Enter 使用默认设置就好了。你需要输入的就是你网站的名字、网站的作者,另外就是当问到 URL prefix(URL 前缀) 和 timezone(时区) 的时候选 n。下边是个例子:

(jupyter-blog)➜  jupyter-blog ✗ pelican-quickstart
Welcome to pelican-quickstart v3.6.3.

This script will help you create a new Pelican-based website.

Please answer the following questions so this script can generate the files
needed by Pelican.


> Where do you want to create your new web site? [.]
> What will be the title of this web site? Vik's Blog
> Who will be the author of this web site? Vik Paruchuri
> What will be the default language of this web site? [en]
> Do you want to specify a URL prefix? e.g., http://example.com   (Y/n) n
> Do you want to enable article pagination? (Y/n)
> How many articles per page do you want? [10]
> What is your time zone? [Europe/Paris] America/Los_Angeles
> Do you want to generate a Fabfile/Makefile to automate generation and publishing? (Y/n)
> Do you want an auto-reload & simpleHTTP script to assist with theme and site development? (Y/n)
> Do you want to upload your website using FTP? (y/N)
> Do you want to upload your website using SSH? (y/N)
> Do you want to upload your website using Dropbox? (y/N)
> Do you want to upload your website using S3? (y/N)
> Do you want to upload your website using Rackspace Cloud Files? (y/N)
> Do you want to upload your website using GitHub Pages? (y/N)

运行完 pelican-quickstart 以后,jupyter-blog 文件夹里多了两个文件夹 contentoutput,还有一些文件,比如 pelicanconf.pypublishconf.py。下边是文件夹目录的示例:

jupyter-blog
│   output
│   content
│   .gitignore
│   develop_server.sh
│   fabfile.py
│   Makefile
│   requirements.txt
│   pelicanconf.py
│   publishconf.py

安装 Jupyter 插件

Pelican 默认不支持使用 Jupyter 写文章,所以我们需要安装一个插件来完成这项功能。我们把插件作为一个 git 子模块(git submodule)来安装,这样便于管理。如果你还没有安装 git,你可以在这里找到安装说明。当你安装完成 git 之后:

现在应该会有一个 .gitmodules 文件和一个 plugins 文件夹:

jupyter-blog
│   output
│   content
│   plugins
│   .gitignore
│   .gitmodules
│   develop_server.sh
│   fabfile.py
│   Makefile
│   requirements.txt
│   pelicanconf.py
│   publishconf.py

为了激活插件,我们需要修改 pelicanconf.py 文件,在最下边添加几行代码:

MARKUP = ('md', 'ipynb')

PLUGIN_PATH = './plugins'
PLUGINS = ['ipynb.markup']

这几行代码告诉 Pelican 当生成 HTML 的时候激活插件。

写第一篇文章

插件安装完之后,就可以写你的第一篇文章了:

Title: First Post
Slug: first-post
Date: 2016-06-08 20:00
Category: posts
Tags: python firsts
Author: Vik Paruchuri
Summary: My first post, read it to find out.

这里以上字段的解释:

每发布一篇文章,就需要复制一个 notebook 文件,并创建一个 ipynb-meta 文件

创建好 notebook 和 meta 文件后,就可以生成博客 HTML 文件了。下边是 jupyter-blog 文件夹现在的样子:

jupyter-blog
│   output
│   content
    │   first-post.ipynb
    │   first-post.ipynb-meta
│   plugins
│   .gitignore
│   .gitmodules
│   develop_server.sh
│   fabfile.py
│   Makefile
│   requirements.txt
│   pelicanconf.py
│   publishconf.py

生成 HTML

为了从文章生成 HTML,我们需要先运行 Pelican 来把 notebooks 转换为 HTML,然后运行本地服务器来查看:

在浏览器里就可以看到博客里所有文章的列表,以及具体的博客内容了。

创建 GitHub Pages

GitHub Pages 是 GitHub 的一项功能,允许你快速部署静态网站,让所有人都可以通过特定 URL 访问。为了完成它的配置,我们需要:

GitHub Pages 会把 username.github.io 仓库的 master 分支下的所有 HTML 文件展示到 username.github.io 这个地址(仓库和 URL 是一样的)。

首先我们需要修改 Pelican 使得 URL 指向正确的位置:

提交文件

如果你想把 notebooks 和其他文件作为一个 GitHub Page 放在同一个仓库里,你可以使用分支。

部署到 GitHub Pages

为了让 Github Pages 正常工作,我们需要把文章添加到 master 分支中。现在,HTML 内容在 output 文件夹中,但是我们需要把它放到仓库的根目录,而不是子目录。我们可以使用 ghp-import 工具来完成这项工作:

修改博客后,只要重新运行 pelican content -s publishconf.py, ghp-importgit push,你的 GitHub Page 就会更新了。

下一步

终于搭建好了!你现在可以创作博客,然后推送到 GitHub Pages。所有人都可以通过 username.github.io 来访问你的博客(记得把 username 替换为你的 GitHub 用户名)。这给你提供了一个展示数据科学作品集的渠道。

随着文章数和读者越来越多,你可能就需要在以下方面更深入的研究一下:

上一篇下一篇

猜你喜欢

热点阅读