博文作者 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" >
<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 ></p>
</div>
<hr>
<div class="project project-info">
<div class="row">
<div class="col-md-5" >
<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,喜歡的可以點個收藏和關注,我們下次再見!