python 生成html文件浏览器_Handout库:能将python脚本转化为html展示文件
有的時候我們需要將python代碼進行展示講解,這個時候使用py文件進行講解效果并不是最好的。如果能轉(zhuǎn)化為html文件,在瀏覽器中展示,那就完美了。好消息是存在一個名為handout的庫可以實現(xiàn)我們的設(shè)想。
安裝
pip3 install
U handout
快速學(xué)習(xí)
下面是demo.py文件中的代碼及注釋,其中handout庫可以將注釋部分中的markdown標記轉(zhuǎn)化為html相應(yīng)的樣式
"""
# Python Handout庫
將python腳本轉(zhuǎn)化為帶markdown標記形式的html文件
"""
import
handout
import
matplotlib
.
pyplot
as
plt
import
numpy
as
np
"""## 定義輸出的文件夾"""
doc
=
handout
.
Handout
(
'output'
)
"""
## Markdown注釋
以前后3個"
內(nèi)的部分作為
markdown
待識別區(qū)域,可以用
markdown
語法書寫注釋
例如,
handout
中出現(xiàn)下面的無序列表
-
Headlines
-
Hyperlinks
-
Inline
`code()`
snippets
-
**
Bold
**
and
*
italic
*
"""
"""
## 添加文本和變量
注意這里使用
doc
.
add_text
方法向
handout
中添加運行結(jié)果,類似于
python
中的
"""
for
index
in
range
(
3
):
doc
.
add_text
(
'Iteration'
,
index
)
doc
.
show
()
"""
## 添加matplotlib圖
在handout中添加matplotlib圖
"""
fig
,
ax
=
plt
.
subplots
(
figsize
=(
4
,
3
))
ax
.
plot
(
np
.
arange
(
100
))
fig
.
tight_layout
()
doc
.
add_figure
(
fig
)
doc
.
show
()
"""
設(shè)置handout中圖片的尺寸
"""
for
iteration
in
range
(
3
):
fig
,
ax
=
plt
.
subplots
(
figsize
=(
3
,
2
))
ax
.
plot
(
np
.
sin
(
np
.
linspace
(
0
,
20
/
(
iteration
+
1
),
100
)))
doc
.
add_figure
(
fig
,
width
=
0.33
)
doc
.
show
()
"""
## 添加圖片
This requires the `imageio` pip package.
"""
image_a
=
np
.
random
.
uniform
(
0
,
255
,
(
200
,
400
,
3
)).
astype
(
np
.
uint8
)
image_b
=
np
.
random
.
uniform
(
0
,
255
,
(
100
,
200
,
1
)).
astype
(
np
.
uint8
)
doc
.
add_image
(
image_a
,
'png'
,
width
=
0.4
)
doc
.
add_image
(
image_b
,
'jpg'
,
width
=
0.4
)
doc
.
show
()
"""
## 瀏覽handout
默認doc.show()輸出到output文件夾中的index.html文件
"""
輸出結(jié)果
下面左側(cè)是代碼,右側(cè)是轉(zhuǎn)化后的html文件效果。
下面是demo.py文件的運行過程及結(jié)果的動態(tài)展示
總結(jié)
以上是生活随笔為你收集整理的python 生成html文件浏览器_Handout库:能将python脚本转化为html展示文件的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: pxe自动部署Linux,Kicksta
- 下一篇: 修改软件服务器json返回数据格式,An