python 生成html文件浏览器_Handout库:能将python脚本转化为html展示文件

有的时候我们需要将python代码进行展示讲解,这个时候使用py文件进行讲解效果并不是最好的。如果能转化为html文件,在浏览器中展示,那就完美了。好消息是存在一个名为handout的库可以实现我们的设想。

安装

pip3 install

U handout

快速学习

下面是demo.py文件中的代码及注释,其中handout库可以将注释部分中的markdown标记转化为html相应的样式

"""

# Python Handout库

将python脚本转化为带markdown标记形式的html文件

"""

import

handout

import

matplotlib

.

pyplot

as

plt

import

numpy

as

np

"""## 定义输出的文件夹"""

doc

=

handout

.

Handout

(

'output'

)

"""

## Markdown注释

以前后3个"

内的部分作为

markdown

待识别区域,可以用

markdown

语法书写注释

例如,

handout

中出现下面的无序列表

-

Headlines

-

Hyperlinks

-

Inline

`code()`

snippets

-

**

Bold

**

and

*

italic

*

"""

"""

## 添加文本和变量

注意这里使用

doc

.

add_text

方法向

handout

中添加运行结果,类似于

python

中的

print

"""

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

()

"""

设置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文件

"""

输出结果

下面左侧是代码,右侧是转化后的html文件效果。

下面是demo.py文件的运行过程及结果的动态展示

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值