Bowen author wangzirui32
Like can give the thumbs-up Collection Pay attention to ~~
This article was first published in CSDN, Reprint is prohibited without permissionhello, Hello everyone , I am a wangzirui32, Today we're going to learn how to use PyGithub+jinja2 Generate Github Project Poster , Start learning !
We need to install PyGithub
and jinja2
Third party Library ,PyGithub
It is used to obtain the basic information of the repository ,jinja2
Used to generate HTML posters , Installation command :
pip install pygithub jinja2
We created app.py
,get_data.py
and repository.html
,get_data.py
Used to obtain repository data ,app.py
Will pass through HTML Templates repository.html
Generate Poster , It's done.
get_data.py
The code is as follows :
from github import Github
from datetime import datetime
def get_data(username="wangzirui32", repo="wangzirui32/wzr_spider"):
g = Github() # Definition Github object
repo = g.get_repo(repo) # Get repository
return {
"username": username, # user name
"post_time": datetime.now().strftime("%Y-%m-%d"), # Poster creation time
"name": repo.full_name, # full name
"desc": repo.description, # explain
"created_date": repo.created_at, # Creation time
"last_push": repo.pushed_at, # Last submission date
"home_page": repo.homepage, # Project home page
"language": repo.language, # programing language
"forks": repo.forks, # Number of branches
"stars": repo.stargazers_count # Star number
}
if __name__ == '__main__':
from pprint import pprint
pprint(get_data())
app.py
Code :
from jinja2 import Template
from data import get_data
def create_page(data):
# Read the template
t = Template(open("repository.html", encoding="utf-8").read())
# Return rendering results
return t.render(data)
if __name__ == '__main__':
# write in post.html
with open("post.html", "w", encoding="utf-8") as f:
f.write(create_page(get_data()))
repository.html
Is the template of the poster , Applied to Bootstarp The front frame , Code :
<!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 Repository Poster - {
{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> Project brief introduction </h2>
<p class="text">{
{desc}}</p>
</div>
<div class="col-md-6">
<h2> Project data </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>
Put all files in the same directory , Install the required packages to run app.py
,post.html
The effect is as follows :
Okay , That's all for today's lesson , I am a wangzirui32, You can collect and pay attention to what you like , See you next time !