pyecharts如何添加自定义图片

今天为了往pyecharts产生的html里添加微信二维码费了九牛二虎之力,主要是因为pyecharts的文档非常烂!

微信二维码
wechat QR
QR = myqr.run(
     words="https://geoseis.cn/CoV_en.html",          # 可以是字符串,也可以是网址(前面要加http(s)://)
     version=1,                              # 设置容错率为最高
     level='H',                              # 控制纠错水平,范围是L、M、Q、H,从左到右依次升高
     picture="C:/Users/xuzhen/Pictures/0.jpg",                           # 将二维码和图片合成
     colorized=True,                         # 彩色二维码
     contrast=1.0,                           #用以调节图片的对比度,1.0 表示原始图片,更小的值表示更低对比度,更大反之。默认为1.0
     brightness=1.0,                         #用来调节图片的亮度,其余用法和取值同上
     save_name="C:/Users/xuzhen/Pictures/python.gif",                     # 保存文件的名字,格式可以是jpg,png,bmp,gif
     save_dir=os.getcwd()                    #控制位置
 )
def image_base() -> Image:
     image = Image()
img_src = (
    "https://www.geoseis.cn/wp-content/gallery/pycharm/"
    "python.gif"
)
image.add(
    src=img_src,
    style_opts={"width": "200px", "height": "200px", "style": "margin-top: 10px"},
).set_global_opts(
    title_opts=ComponentTitleOpts(title="WeChat QR",title_style={"style": "font-size: 18px; font-weight:bold; color:red"},\
                                  subtitle="Scan to follow the realtime update!",subtitle_style={"style": "font-size: 12px; color:red"})
)
return image
  • 添加到page函数(重点是add(image_base(),如果没有这个括号是不行的,pyecharts没有这种实例需要自己琢磨它的用法):
page = (Page(page_title="2019-nCov")
        .add(world_map)
        .add(total_pie)
        .add(area_map)
        .add(area_heat_geo)
        .add(bar)
        .add(big_title)
        .add(times)
        .add(confirms)
        .add(confirms_people)
        .add(suspects)
        .add(suspects_people)
        .add(deads)
        .add(deads_people)
        .add(heals)
        .add(heals_people)
        .add(confirm_liquid)
        .add(suspect_liquid)
        .add(dead_liquid)
        .add(heal_liquid)
        .add(wc)
        .add(image_base())
        ).render("2019-nCov-RealTime.html")
  • 划重点,如何改变图片标题的字体(文档没有说明,需要阅读源码自己琢磨下),源码就在Options下的Charts_options.py:
title_opts=ComponentTitleOpts(title="WeChat QR",title_style={"style": "font-size: 18px; font-weight:bold; color:red"},\
subtitle="Scan to follow the realtime update!",subtitle_style={"style": "font-size: 12px; color:red"})
)

C:\Users\xuzhen\env\pyecharts\Lib\site-packages\pyecharts\options

最后的效果图:

肺炎疫情BI仪表盘

Python 脚本自启动及定时任务

爬虫脚本定时运行(crontab)

linux定时执行python脚本的方法以及失败不执行的处理方法

https://www.jianshu.com/p/5cd74add11ba 没有采用简书的方案,上面的博客方案更加清晰。以下是我的笔记(重点是环境变量,路径,scp的使用方法)

不需要root,执行以下命令:
crontab -e
编辑:
#注意这里最好把环境变量写上
PATH=/sbin:/bin:/usr/sbin:/usr/bin:/home/chen/miniconda3/bin/
MAILTO=root
#测试:两分钟执行一次
*/2 * * * * echo "crontab test" >> /home/chen/ctest.txt
#写的python脚本每5分钟执行一次(注意python脚本中文件路径必须是绝对路径,否则报错)
*/5 * * * * /home/chen/miniconda3/bin/python /home/chen/CoV_copy2.py 
#scp python脚本产生的HTML到博客服务器(这里用了密钥而不是密码)。注意:所有的路径都#使用绝对路径,否则无效。
* * * * * scp -i /home/chen/.ssh/do248 -P 4705 /home/chen/2019-nCov数据一览2.html chen@207.148.96.113:/home/wwwroot/geoseis.cn/CoV.html >> /home/chen/cov.log
# For details see man 4 crontabs

# Example of job definition:
# .---------------- minute (0 - 59)
# |  .------------- hour (0 - 23)
# |  |  .---------- day of month (1 - 31)
# |  |  |  .------- month (1 - 12) OR jan,feb,mar,apr ...
# |  |  |  |  .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# |  |  |  |  |
# *  *  *  *  * user-name  command to be executed

linux定时执行失败的处理方法就是查看 /var/spool/mail/ ,一般root里都有日志,可以看到哪里出错了。

这个爬虫脚本放到vpn后需要更新很多:

  • 文件路径必须为绝对路径
  • pyecharts的Geo函数bug:遇到不存在的地点Geo函数就会使程序退出,即使用了except也不行,和笔记本环境不一样。笔记本里运行程序不会退出。解决方案就是复制了旧版本的一些.py程序到相应的目录
  • proxy在VPN中没有设置好,因为笔记本里用的SSR代理的(5个VPN)。这个脚本五分钟执行一次不需要代理也可以

scp的使用

scp推荐使用密钥传输,更加安全。密钥产生后我直接append到目标服务器的/home/chen/.ssh/authorized_keys,然后scp -i 就可以无密码传输了。

运行 ssh-keygen -t dsa 时最好设置密码,否则密钥没有密码也不安全。

参考:

It’s better to set up ssh to used key-based authentication rather than trying to figure out how to send text to the login process with something like expect.

Take a look at:

https://help.ubuntu.com/community/SSH/OpenSSH/Keys

So, basically, run ssh-keygen -t dsa on the machine that will run your script. When it asks you for a passphrase, hit ENTER to accept a blank. You will get two files. If you followed the default suggestions, the files will be ~/.ssh/id_dsa and ~/.ssh/id_dsa.pub. The first one is the private key. The second one is the public key.

Copy the public key to the second server using ssh-copy-id user@server2. This will add the public key to the authorized_keys file of the user on server2.

You should now be able to run ssh from the first machine and log in without a password.

For copying the files, scp or rsync are fine. It depends on what you’re doing. rsync will use ssh by default, so will use the key-based authentication you just set up