Giter VIP home page Giter VIP logo

pybadges's Introduction

CircleCI pypi versions

pybadges

pybadges is a Python library and command line tool that allows you to create Github-style badges as SVG images. For example:

pip installation pip installation pip installation

The aesthetics of the generated badges matches the visual design found in this specification.

The implementation of the library was heavily influenced by Shields.io and the JavaScript badge-maker library.

Getting Started

Installing

pybadges can be installed using pip:

pip install pybadges

To test that installation was successful, try:

python -m pybadges --left-text=build --right-text=failure --right-color='#c00' --browser

You will see a badge like this in your browser:

pip installation

Usage

pybadges can be used both from the command line and as a Python library.

The command line interface is a great way to experiment with the API before writing Python code.

You could also look at the example server.

Command line usage

Complete documentation of pybadges command arguments can be found using the --help flag:

python -m pybadges --help

But the following usage demonstrates every interesting option:

python -m pybadges \
    --left-text=complete \
    --right-text=example \
    --left-color=green \
    --right-color='#fb3' \
    --left-link=http://www.complete.com/ \
    --right-link=http://www.example.com \
    --logo='data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAIAAAACCAIAAAD91JpzAAAAD0lEQVQI12P4zwAD/xkYAA/+Af8iHnLUAAAAAElFTkSuQmCC' \
    --embed-logo \
    --whole-title="Badge Title" \
    --left-title="Left Title" \
    --right-title="Right Title" \
    --browser

pip installation

A note about --logo and --embed-logo

Note that the --logo option can include a regular URL:

python -m pybadges \
    --left-text="python" \
    --right-text="3.2, 3.3, 3.4, 3.5, 3.6" \
    --whole-link="https://www.python.org/" \
    --browser \
    --logo='https://dev.w3.org/SVG/tools/svgweb/samples/svg-files/python.svg'

pip installation

If the --logo option is set, the --embed-logo option can also be set. The --embed-logo option causes the content of the URL provided in --logo to be embedded in the badge rather than be referenced through a link.

The advantage of using this option is an extra HTTP request will not be required to render the badge and that some browsers will not load image references at all.

You can see the difference in your browser:

--embed-logo=yes --embed-logo=no

A note about --(whole|left|right)-title

The title element is usually displayed as a pop-up by browsers but is currently filtered by Github.

Library usage

pybadges is primarily meant to be used as a Python library.

from pybadges import badge
s = badge(left_text='coverage', right_text='23%', right_color='red')
# s is a string that contains the badge data as an svg image.
print(s[:40]) # => <svg height="20" width="191.0" xmlns="ht

The keyword arguments to badge() are identical to the command flags names described above except with keyword arguments using underscore instead of hyphen/minus (e.g. --left-text => left_text=)

Server usage

pybadges can be used to serve badge images on the web.

server-example contains an example of serving badge images from a Flask server.

Caveats

  • pybadges uses a pre-calculated table of text widths and kerning distances (for western glyphs) to determine the size of the badge. So Eastern European languages may be rendered less well than Western European ones:

    pip installation

    and glyphs not present in Deja Vu Sans (the default font) may be rendered very poorly:

    pip installation

  • pybadges does not have any explicit support for languages that are written right-to-left (e.g. Arabic, Hebrew) and the displayed text direction may be incorrect:

    pip installation

Development

git clone https://github.com/google/pybadges.git
cd pybadges
python -m virtualenv venv
source venv/bin/activate
# Installs in edit mode and with development dependencies.
pip install -e .[dev]
nox

If you'd like to contribute your changes back to pybadges, please read the contributor guide.

Versioning

We use SemVer for versioning.

License

This project is licensed under the Apache License - see the LICENSE file for details

This is not an officially supported Google product.

pybadges's People

Contributors

alexeyder avatar brianquinlan avatar calebcartwright avatar chantsune avatar jason-cooke avatar kkapka avatar kotlinisland avatar kshithijiyer avatar nshan651 avatar rdbisme avatar sofignatova avatar ylil93 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

pybadges's Issues

Logo Disappears

Hello!
I am trying to use the python logo:

in one of the badges; even when downloading it and using it in the command, I cannot seem to get it to display anywhere except when it is situated on my computer and shown in my browser.
The code I am using to create it:

/home/shadowrylander/miniconda3/envs/pypy3/bin/pypy3 -m pybadges \
    --left-text=t1 \
    --right-text=t2 \
    --left-color=yellow \
    --right-color=blue \
    --logo="https://upload.wikimedia.org/wikipedia/commons/c/c3/Python-logo-notext.svg" \
    --browser

This is how I'm seeing it:
https://www.dropbox.com/s/wixt0mpx9ne93id/pypy3.svg?dl=0
So similar to the python example in the README.md with the regular url.
Thank you kindly for the help!

Python 2.7

Works great with Python 3.6, no luck with 2.7 though. Fails with an ugly error message:

Traceback (most recent call last):
File "/usr/lib/python2.7/runpy.py", line 163, in _run_module_as_main
mod_name, _Error)
File "/usr/lib/python2.7/runpy.py", line 111, in _get_module_details
import(mod_name) # Do not catch exceptions initializing package
File "/home/uli/projects/badges1/local/lib/python2.7/site-packages/pybadges/init.py", line 74
def badge(left_text: str, right_text: str, left_link: Optional[str] = None,
^
SyntaxError: invalid syntax

Add User-Agent option for Logo Lookup

Trying to source logos for example from wikipedia can run into:

requests.exceptions.HTTPError: 403 Client Error: Forbidden. Please comply with the User-Agent policy: https://meta.wikimedia.org/wiki/User-Agent_policy

Which states that there must be user-agent information or scripts may be blocked without notice. The expected format is

<client name>/<version> (<contact information>) <library/framework name>/<version> [<library name>/<version> ...]

They provide a template for python:

import requests

url = 'https://example/...'
headers = {'User-Agent': 'CoolBot/0.0 (https://example.org/coolbot/; [email protected])'}

response = requests.get(url, headers=headers)

Suggestions:

  • Add standard header like {'User-Agent': 'pybadges/3.0.0 (https://github.com/google/pybadges/)}
  • And/Or add --user-agent option to allow passing this information manually.

Upgrade to Flask 2.0?

This project has a dependency on jinja2, which is preventing our service from updating to Flask 2.0.

We'd love to continue using pybadges, but may need to replace it in order to upgrade if a new maintainer does not come forward.

Custom Styles

Is there any way we could have other styles like 'style=flat-square '?

ModuleNotFoundError: No module named '_lzma'

Got ModuleNotFoundError when using this package:

Python 3.6.3 (default, Feb 16 2018, 11:33:20) 
[GCC 6.3.0 20170516] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from pybadges import badge
>>> s = badge(left_text='coverage', right_text='23%', right_color='red')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/google/home/yanhuil/fork/cloud-opensource-python/.tox/py36/lib/python3.6/site-packages/pybadges/__init__.py", line 116, in badge
    precalculated_text_measurer.PrecalculatedTextMeasurer
  File "/usr/local/google/home/yanhuil/fork/cloud-opensource-python/.tox/py36/lib/python3.6/site-packages/pybadges/precalculated_text_measurer.py", line 79, in default
    import lzma
  File "/usr/local/lib/python3.6/lzma.py", line 27, in <module>
    from _lzma import *
ModuleNotFoundError: No module named '_lzma'

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.