【问题标题】:Display GitHub stats on a Flask website在 Flask 网站上显示 GitHub 统计信息
【发布时间】:2017-04-20 00:44:27
【问题描述】:

我想知道是否有一种方法可以将存储库中提交/分支/等的数量显示到网站。我正在使用 Flask 在 apache2 服务器上使用 jinja2 的网站,而不是通过硬编码显示数字,我希望它在进行新的提交/分支时自动更新数字。

【问题讨论】:

    标签: python api github flask jinja2


    【解决方案1】:

    您可以使用GitHub API 来执行此操作。

    例如,您可以通过请求 (GET) /repos/:owner/:repo/commits 来请求存储库的提交数量。

    响应示例如下(来自 API 文档):

    [
      {
        "url": "https://api.github.com/repos/octocat/Hello-World/commits/6dcb09b5b57875f334f61aebed695e2e4193db5e",
        "sha": "6dcb09b5b57875f334f61aebed695e2e4193db5e",
        "html_url": "https://github.com/octocat/Hello-World/commit/6dcb09b5b57875f334f61aebed695e2e4193db5e",
        "comments_url": "https://api.github.com/repos/octocat/Hello-World/commits/6dcb09b5b57875f334f61aebed695e2e4193db5e/comments",
        "commit": {
          "url": "https://api.github.com/repos/octocat/Hello-World/git/commits/6dcb09b5b57875f334f61aebed695e2e4193db5e",
          "author": {
            "name": "Monalisa Octocat",
            "email": "support@github.com",
            "date": "2011-04-14T16:00:49Z"
          },
          "committer": {
            "name": "Monalisa Octocat",
            "email": "support@github.com",
            "date": "2011-04-14T16:00:49Z"
          },
          "message": "Fix all the bugs",
          "tree": {
            "url": "https://api.github.com/repos/octocat/Hello-World/tree/6dcb09b5b57875f334f61aebed695e2e4193db5e",
            "sha": "6dcb09b5b57875f334f61aebed695e2e4193db5e"
          },
          "comment_count": 0,
          "verification": {
            "verified": true,
            "reason": "valid",
            "signature": "-----BEGIN PGP MESSAGE-----\n...\n-----END PGP MESSAGE-----",
            "payload": "tree 6dcb09b5b57875f334f61aebed695e2e4193db5e\n..."
          }
        },
        "author": {
          "login": "octocat",
          "id": 1,
          "avatar_url": "https://github.com/images/error/octocat_happy.gif",
          "gravatar_id": "",
          "url": "https://api.github.com/users/octocat",
          "html_url": "https://github.com/octocat",
          "followers_url": "https://api.github.com/users/octocat/followers",
          "following_url": "https://api.github.com/users/octocat/following{/other_user}",
          "gists_url": "https://api.github.com/users/octocat/gists{/gist_id}",
          "starred_url": "https://api.github.com/users/octocat/starred{/owner}{/repo}",
          "subscriptions_url": "https://api.github.com/users/octocat/subscriptions",
          "organizations_url": "https://api.github.com/users/octocat/orgs",
          "repos_url": "https://api.github.com/users/octocat/repos",
          "events_url": "https://api.github.com/users/octocat/events{/privacy}",
          "received_events_url": "https://api.github.com/users/octocat/received_events",
          "type": "User",
          "site_admin": false
        },
        "committer": {
          "login": "octocat",
          "id": 1,
          "avatar_url": "https://github.com/images/error/octocat_happy.gif",
          "gravatar_id": "",
          "url": "https://api.github.com/users/octocat",
          "html_url": "https://github.com/octocat",
          "followers_url": "https://api.github.com/users/octocat/followers",
          "following_url": "https://api.github.com/users/octocat/following{/other_user}",
          "gists_url": "https://api.github.com/users/octocat/gists{/gist_id}",
          "starred_url": "https://api.github.com/users/octocat/starred{/owner}{/repo}",
          "subscriptions_url": "https://api.github.com/users/octocat/subscriptions",
          "organizations_url": "https://api.github.com/users/octocat/orgs",
          "repos_url": "https://api.github.com/users/octocat/repos",
          "events_url": "https://api.github.com/users/octocat/events{/privacy}",
          "received_events_url": "https://api.github.com/users/octocat/received_events",
          "type": "User",
          "site_admin": false
        },
        "parents": [
          {
            "url": "https://api.github.com/repos/octocat/Hello-World/commits/6dcb09b5b57875f334f61aebed695e2e4193db5e",
            "sha": "6dcb09b5b57875f334f61aebed695e2e4193db5e"
          }
        ]
      }
    ]
    

    由于您使用的是 API,因此您可以在每次加载页面时请求它,从某种意义上说,自动更新它。

    【讨论】:

      猜你喜欢
      • 2010-09-06
      • 2021-05-28
      • 1970-01-01
      • 2013-11-12
      • 2013-04-02
      • 1970-01-01
      • 1970-01-01
      • 2021-03-03
      • 1970-01-01
      相关资源
      最近更新 更多