编程派微信号:codingpycss
这是「打造数据科学做品集」系列文章的第二篇。若是你喜欢该系列,并且想知道下一篇文章何时发布,你能够订阅咱们。读完本文,你将学会如何使用 Pelican 静态网站生成器,搭建一个属于本身的博客,用来展现数据科学做品。html
全文大约 9800 字,读完可能须要 15 分钟左右。python
你能够在这里阅读本系列第一篇文章:「打造数据科学做品集:用数据讲故事」git
写博客是证实你的实力、深刻学习和创建读者群的好方法。有许多数据科学和编程类博客帮助他们的做者找到工做,或者认识了重要人物。按期写博客是有抱负的程序员和数据科学家最应该作的事情之一。程序员
不幸的是,写博客的一大障碍就是先搭建一个博客网站。在这篇文章中,咱们将学习如何用 Python 建立一个博客网站,怎么用 Jupyter Notebook 写文章和如何经过 GitHub Pages 部署博客。读完这篇文章,你就可使用你熟悉的方式,建立本身的数据科学博客了。github
基本上,一个静态网站就是一个全是 HTML 文件的文件夹。咱们能够搭建一个容许别人连接到这个文件夹并获取文件的服务器。这样作的好处是不须要数据库或者其余动态部分,能够很简单的部署在像 GitHub 之类的网站上。把你的博客作成静态网站是一个好主意,由于维护起来十分简单。创建静态网站的一种方法是手写 HTML,而后上传全部的 HTML 文件到服务器。这种状况下,你至少要写一个 index.html
文件。若是你的网站的 URL 是 thebestblog.com
,当访问者浏览 http://www.thebestblog.com
时,他们就会看到 index.html
的内容了。 HTML 的文件夹多是下边的这个样子:web
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 会很枯燥。浏览器
若是你想写多篇文章,你须要复制不少内容,好比样式、Title、Footer 等。
若是你想整合评论系统或者其余插件,你不得不写 Javascript。
通常来讲,你写博客的时候,想关注于博客内容,而不是在 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 让别人阅读。
开始以前,这里有一个仓库(repo),它就是咱们最终成果的示例。
若是你尚未安装 Python,在开始以前你还须要作一些前期工做。这里有一些安装 Python 的说明。咱们建议使用 Python3.5。当你安装完成 Python:
建立一个文件夹——咱们将把博客网站的内容和样式(Styles)放在这个文件夹里。该教程把这个文件夹叫作 jupyter-blog
,你能够随便起名字。
cd
进入 jupyter-blog
文件夹。
建立一个叫 .gitignore
的文件,而后把这个文件里的内容加进去。咱们最后将要把仓库提交到 git,而这将会排除一些其余东西。
建立并激活一个虚拟环境。
在 jupyter-blog
文件夹里建立一个叫 requirements.txt
的文件,内容以下:
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
文件夹里运行 pip install -r requirements.txt
来安装 requirements.txt
里边全部的包。
完成了前边的设置以后,你就作完建立博客的准备了!在 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
文件夹里多了两个文件夹 content
和 output
,还有一些文件,好比 pelicanconf.py
和 publishconf.py
。下边是文件夹目录的示例:
jupyter-blog │ output │ content │ .gitignore │ develop_server.sh │ fabfile.py │ Makefile │ requirements.txt │ pelicanconf.py │ publishconf.py
Pelican 默认不支持使用 Jupyter 写文章,因此咱们须要安装一个插件来完成这项功能。咱们把插件做为一个 git 子模块(git submodule)来安装,这样便于管理。若是你尚未安装 git,你能够在这里找到安装说明。当你安装完成 git 以后:
运行 git init
来把当前文件夹初始化为一个 git 仓库。
建立一个叫 plugins
的文件夹。
运行 git submodule add git://github.com/danielfrg/pelican-ipynb.git plugins/ipynb
来添加插件。
如今应该会有一个 .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 的时候激活插件。
插件安装完以后,就能够写你的第一篇文章了:
建立一个 Jupyter notebook,简单写一些内容。这里有一个例子。
把 notebook 文件复制到 content
文件夹。
建立一个和 notebook
同名的文件,可是扩展名是 .ipynb-meta
。这里有一个例子。
把下边的内容添加到 ipynb-meta
文件中,可是根据你本身的文章修改相应字段:
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.
这里以上字段的解释:
Title
——文章的标题。
Slug
——你的文章在服务器上的路径。若是 slug 是 first-post
,并且你的服务器地址是 jupyter-blog.com
, 你能够在 http://www.jupyter-blog.com/first-post
这个地址找到你的文章。
Date
——文章发布的日期。
Category
——文章的类别——能够是任何东西。
Tags
——文章的标签。能够随便挂标签。
Author
——文章做者的名字。
Summary
——文章的摘要。
每发布一篇文章,就须要复制一个 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,咱们须要先运行 Pelican 来把 notebooks 转换为 HTML,而后运行本地服务器来查看:
切换到 jupyter-blog
文件夹。
运行pelican content
来生成 HTML。
切换到 output
目录。
运行python -m pelican.server
。
在浏览器里访问 localhost:8000
来预览你的博客。
在浏览器里就能够看到博客里全部文章的列表,以及具体的博客内容了。
GitHub Pages 是 GitHub 的一项功能,容许你快速部署静态网站,让全部人均可以经过特定 URL 访问。为了完成它的配置,咱们须要:
注册一个 GitHub 账号,若是你尚未的话。
建立一个叫 username.github.io
的仓库,这里 username
是你的 GitHub 用户名。这里有更详细的说明告诉你怎么作。
切换到 jupyter-blog
文件夹。
运行 git remote add origin git@github.com:username/username.github.io.git
把这个仓库做为远程仓库添加到你的本地仓库,把全部的 username
参数替换为你的 GitHub 用户名。
GitHub Pages 会把 username.github.io
仓库的 master
分支下的全部 HTML 文件展现到 username.github.io
这个地址(仓库和 URL 是同样的)。
首先咱们须要修改 Pelican 使得 URL 指向正确的位置:
在 publishconf.py
文件里编辑 SITEURL
,把它设置为 http://username.github.io
,username
仍是你的GitHub用户名。
运行 pelican content -s publishconf.py
。当你想在本地预览你的博客的时候,运行 pelican content
。在部署以前运行 pelican content -s publishconf.py
。这将使用正确的配置文件进行部署。
若是你想把 notebooks 和其余文件做为一个 GitHub Page 放在同一个仓库里,你可使用分支。
运行 git checkout dev
切换到一个叫 dev
的分支。咱们不能用 master
分支来存放 notebooks,由于那个分支是用于 GitHub Pages 展现的。
建立一个提交,而后和正常同样推送到 Github(使用 git add
, git commit
,和 git push
)。
为了让 Github Pages 正常工做,咱们须要把文章添加到 master
分支中。如今,HTML
内容在 output
文件夹中,可是咱们须要把它放到仓库的根目录,而不是子目录。咱们可使用 ghp-import
工具来完成这项工做:
运行 ghp-import output -b master
,把 output
目录下的全部东西导入 master
分支。
使用 git push origin master
把你的内容推送到 GitHub。
尝试访问 username.github.io
——你就能够看到你的页面了!
修改博客后,只要从新运行 pelican content -s publishconf.py
, ghp-import
和 git push
,你的 GitHub Page 就会更新了。
终于搭建好了!你如今能够创做博客,而后推送到 GitHub Pages。全部人均可以经过 username.github.io
来访问你的博客(记得把 username 替换为你的 GitHub 用户名)。这给你提供了一个展现数据科学做品集的渠道。
随着文章数和读者愈来愈多,你可能就须要在如下方面更深刻的研究一下:
主题:Pelican 支持主题。在这里你能够看到不少主题,随便选一个你喜欢的用吧。
自定义URL:使用 username.github.io
已经不错了,可是有时候你可能须要自定义域名。这里是自定义 GitHub Pages 域名的指南。
插件:这里有一个插件列表。插件能够帮助你设置网站数据分析,实现评论等功能。
(全文完)
做者:Vik Paruchuri@Dataquest.io
译者:cystone
校对:EarlGrey
出品:PythonTG 翻译组/编程派
欢迎转发至朋友圈。如无特殊注明,本公号所发文章均为原创或编译,如需转载,请联系「编程派」得到受权。