程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

[Python] pygithub+jinja2 generate a simple poster of GitHub project

編輯:Python

Bowen author wangzirui32
Like can give the thumbs-up Collection Pay attention to ~~
This article was first published in CSDN, Reprint is prohibited without permission


hello, Hello everyone , I am a wangzirui32, Today we're going to learn how to use PyGithub+jinja2 Generate Github Project Poster , Start learning !

Catalog

  • 1. Third party library ready
  • 2. Project ideas
  • 3. get_data.py
  • 4. app.py
  • 5. repository.html
  • 6. Project effect

1. Third party library ready

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

2. Project ideas

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.

3. get_data.py

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())

4. app.py

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()))

5. repository.html

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>

6. Project effect

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 !


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved