Skip to content

Commit 70cfe2e

Browse files
committed
修改了生成静态文件的代码
Former-commit-id: 8c96c50
1 parent 71f54fa commit 70cfe2e

File tree

3 files changed

+87
-9
lines changed

3 files changed

+87
-9
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ conda update anaconda
5454

5555
可以在[nbviewer](http://nbviewer.ipython.org/github/lijin-THU/python-tutorial/blob/master/index.ipynb)中查看该项目。
5656

57-
可以在 static files 文件夹查看 [HTML 文件](static files/html/README.md)
57+
可以使用 notebook "`generate static files.ipynb`" 或者执行 `Python` 代码 "`generate_static_files.py`" 来生成静态的 `HTML` 文件
5858

5959
----
6060

generate_static_files.py

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
2+
# coding: utf-8
3+
4+
# # 将笔记转化为不同的文件格式
5+
6+
# In[1]:
7+
8+
import os
9+
import os.path
10+
import nbconvert
11+
12+
13+
# 检查路径是否存在:
14+
15+
# In[2]:
16+
17+
if not os.path.exists('static files'):
18+
os.mkdir('static files')
19+
20+
21+
# 文件夹:
22+
23+
# In[3]:
24+
25+
folders = ['01. python tools',
26+
'02. python essentials',
27+
'03. numpy',
28+
'04. scipy',
29+
'05. advanced python',
30+
'06. matplotlib',
31+
'07. interfacing with other languages',
32+
'08. object-oriented programming',
33+
'09. theano',
34+
'10. something interesting',
35+
'11. useful tools'
36+
]
37+
38+
39+
# 遍历文件夹得到所有的文件名:
40+
41+
# In[4]:
42+
43+
file_names = []
44+
45+
for folder in folders:
46+
files = sorted(os.listdir(folder))
47+
file_names += [os.path.join(folder, file_name) for file_name in files if file_name.endswith('.ipynb')]
48+
49+
50+
# In[5]:
51+
52+
def convert_to_files(names, to_format):
53+
target_dir = os.path.join("static files", to_format)
54+
for folder in folders:
55+
if not os.path.exists(os.path.join(target_dir, folder)):
56+
os.makedirs(os.path.join(target_dir, folder))
57+
converter = {
58+
"html": nbconvert.export_html,
59+
"python": nbconvert.export_python
60+
}
61+
62+
for file_name in names:
63+
p = converter[to_format](file_name)
64+
with open(os.path.join(target_dir, file_name[:-6] + p[1]["output_extension"]), 'w') as f:
65+
f.write(p[0].encode("utf-8"))
66+
print file_name
67+
68+
69+
# 转化 HTML 文件:
70+
71+
# In[6]:
72+
73+
convert_to_files(file_names, "html")
74+
75+
76+
# 产生新目录:
77+
78+
# In[7]:
79+
80+
with open('index.md') as f:
81+
text = f.read()
82+
with open(os.path.join("static files/html", "README.md"), "w") as g:
83+
g.write(text.replace(".ipynb", ".html"))
84+

index.ipynb

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,6 @@
6565
"source": [
6666
"----\n",
6767
"\n",
68-
<<<<<<< HEAD
6968
"## 目录"
7069
]
7170
},
@@ -75,14 +74,9 @@
7574
"source": [
7675
"可以在[nbviewer](http://nbviewer.ipython.org/github/lijin-THU/python-tutorial/blob/master/index.ipynb)中查看该笔记。\n",
7776
"\n",
78-
"可以在 static files 文件夹查看 [HTML 文件](static files/html/README.md)\n",
77+
"可以使用 notebook \"`generate static files.ipynb`\" 或者执行 `Python` 代码 \"`generate_static_files.py`\" 来生成静态的 `HTML` 文件\n",
7978
"\n",
8079
"---"
81-
=======
82-
"## 目录\n",
83-
"\n",
84-
"可以在[nbviewer](http://nbviewer.ipython.org/github/lijin-THU/python-tutorial/blob/master/index.ipynb)中查看该笔记。"
85-
>>>>>>> 9dbc2e9cdb91aacb17cdf700f0639c3488b4dd7b
8680
]
8781
},
8882
{
@@ -235,7 +229,7 @@
235229
"name": "python",
236230
"nbconvert_exporter": "python",
237231
"pygments_lexer": "ipython2",
238-
"version": "2.7.6"
232+
"version": "2.7.11"
239233
}
240234
},
241235
"nbformat": 4,

0 commit comments

Comments
 (0)