✨博文作者 wangzirui32
💖 喜欢的可以 点赞 收藏 关注哦~~
👉本文首发于CSDN,未经许可禁止转载
😎 hello,大家好,我是wangzirui32,今天我们来学习如何使用PyGithub+jinja2生成Github项目海报,开始学习吧!
我们需要安装PyGithub
和jinja2
第三方库,PyGithub
用来获取存储库的基本信息,jinja2
用来生成HTML海报,安装命令:
pip install pygithub jinja2
我们创建app.py
,get_data.py
和repository.html
,get_data.py
用来获取存储库数据,app.py
将通过HTML模板repository.html
生成海报,就完成了
get_data.py
代码如下:
from github import Github
from datetime import datetime
def get_data(username="wangzirui32", repo="wangzirui32/wzr_spider"):
g = Github() # 定义Github对象
repo = g.get_repo(repo) # 获取存储库
return {
"username": username, # 用户名
"post_time": datetime.now().strftime("%Y-%m-%d"), # 海报创建时间
"name": repo.full_name, # 全名
"desc": repo.description, # 说明
"created_date": repo.created_at, # 创建时间
"last_push": repo.pushed_at, # 最后一次提交日期
"home_page": repo.homepage, # 项目主页
"language": repo.language, # 编程语言
"forks": repo.forks, # 分支数
"stars": repo.stargazers_count #星数
}
if __name__ == '__main__':
from pprint import pprint
pprint(get_data())
app.py
代码:
from jinja2 import Template
from data import get_data
def create_page(data):
# 读取模板
t = Template(open("repository.html", encoding="utf-8").read())
# 返回渲染结果
return t.render(data)
if __name__ == '__main__':
# 写入post.html
with open("post.html", "w", encoding="utf-8") as f:
f.write(create_page(get_data()))
repository.html
是海报的模板,运用到了Bootstarp前端框架,代码:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Github 存储库海报 - {{name}}</title>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css" integrity="sha384-HSMxcRTRxnN+Bdg0JdbxYKrThecOKuH5zCYotlSAcp1+c8xmyTe9GYg1l9a69psu" crossorigin="anonymous">
<style>
body {
padding-top: 30px;
height: 100%;
width: 100%;
background-repeat: no-repeat;
background-size: 100% 100%;
background-attachment: fixed;
background-size: cover;
background-image: -moz-linear-gradient(0deg, rgb(72, 209, 204), rgb(194, 238, 255));
background-image: -webkit-linear-gradient(0deg, rgb(72, 209, 204), rgb(194, 238, 255));
background-image: linear-gradient(0deg, rgb(72, 209, 204), rgb(194, 238, 255));
}
.text {
font-size: 18px;
}
.title {
font-size: 45px;
padding-top: 10px;
}
.data-info {
box-shadow: 20px 20px 50px rgba(0,0,0,0.5);
border-radius: 10px;
border-top: 1px solid rgba(255,255,255,0.5);
border-left: 1px solid rgba(255,255,255,0.5);
background: rgba(255,255,255,0.1);
backdrop-filter: blur(5px);
}
.project-info {
box-shadow: 20px 20px 50px rgba(0,0,0,0.5);
border-radius: 10px;
border-top: 1px solid rgba(255,255,255,0.5);
border-left: 1px solid rgba(255,255,255,0.5);
background: rgba(174, 196, 199, 0.5);
backdrop-filter: blur(5px);
margin-left: 10px;
}
</style>
</head>
<body>
{% set url = "https://github.com/" + name %}
<div class="container">
<div class="data-info" style="backdrop-filter: blur(4px);">
<p class="text-center title">{{name}}</p>
<p class="text-center text">
<a href="https://github.com/{{username}}">{{username}}</a>
<span>{{post_time}}</span>
<a href="{{url}}">Github</a>
</p>
<p class="text-center text"><a href="{{url}}">{{url}}</a></p>
<p style="height: 40px;"></p>
</div>
<hr>
<div class="project project-info">
<div class="row">
<div class="col-md-5" style="margin-left: 10px;">
<h2>项目简介</h2>
<p class="text">{{desc}}</p>
</div>
<div class="col-md-6">
<h2>项目数据</h2>
<p><span class="label label-warning text">Stars: {{stars}}</span></p>
<p><span class="label label-success text">Forks: {{forks}}</span></p>
<p><span class="label label-info text">Language: {{language}}</span></p>
<p><span class="label label-primary text">Created at: {{created_date}}</span></p>
<p><span class="label label-danger text">Last push: {{last_push}}</span></p>
{% if home_page %}
<p><span class="label label-primary text">
Home: <a href="{{home_page}}">{{home_page}}</a>
</span></p>
{% endif %}
</div>
</div>
</div>
</div>
</body>
</html>
将所有文件放置在同一目录下,安装所需包运行app.py
,post.html
效果如下:
🎉🎉🎉 好了,今天的课程就到这里,我是wangzirui32,喜欢的可以点个收藏和关注,我们下次再见!