Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/CheeseCake87/pyhead
The Python HTML <head> filler.
https://github.com/CheeseCake87/pyhead
html markup python
Last synced: 4 months ago
JSON representation
The Python HTML <head> filler.
- Host: GitHub
- URL: https://github.com/CheeseCake87/pyhead
- Owner: CheeseCake87
- License: mit
- Created: 2023-11-29T19:31:52.000Z (about 1 year ago)
- Default Branch: master
- Last Pushed: 2024-09-12T20:58:14.000Z (5 months ago)
- Last Synced: 2024-09-13T10:31:50.506Z (5 months ago)
- Topics: html, markup, python
- Language: Python
- Homepage:
- Size: 78.1 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- awesome-python-html - CheeseCake87/pyhead
- awesome-python-html - CheeseCake87/pyhead
README
# pyhead 🐍🤯
[![PyPI version](https://badge.fury.io/py/pyhead.svg)](https://badge.fury.io/py/pyhead)
[![License](https://img.shields.io/badge/license-MIT-blue.svg)](https://raw.githubusercontent.com/CheeseCake87/pyhead/master/LICENSE)
![Downloads](https://static.pepy.tech/badge/pyhead)
![black](https://img.shields.io/badge/code%20style-black-000000.svg)The Python HTML `` filler.
`pip install pyhead`
* [pyhead 🐍🤯](#pyhead-)
* [What is Pyhead?](#what-is-pyhead)
* [Flask example:](#flask-example)
* [Advanced use cases:](#advanced-use-cases)
* [Flask + g](#flask--g)
* [Working with extended templates](#working-with-extended-templates)
* [dict as args, and JSON](#dict-as-args-and-json)
* [CLI Commands](#cli-commands)
* [Generating favicons](#generating-favicons)## What is Pyhead?
Pyhead is a Python package that helps you generate the `` tag for your HTML pages.
## Flask example:
```python
from flask import Flask, render_templatefrom pyhead import Head
def create_app():
app = Flask(__name__)@app.route("/")
def index():
head = Head(
base="https://example.com",
title="Hello World",
exclude_title_tags=True,
# /\ Set to False if returning {{ head() }} in the template
description="This is a test",
keywords="test, hello, world",
subject="Hello World",
rating="General",
robots="index, follow",
)
head.set_default_content_security_policy()
head.set_referrer_policy(
policy="no-referrer",
)
head.set_google(
googlebot="index, follow",
no_sitelinks_search_box=True,
no_translate=True,
)
head.set_verification(
google="1234567890",
yandex="1234567890",
bing="1234567890",
alexa="1234567890",
pinterest="1234567890",
norton="1234567890",
)
head.set_geo_position(
icbm="55.86013028402754, -4.252019430273945",
geo_position="55.86013028402754;-4.252019430273945",
geo_region="en_GB",
geo_placename="Duke of Wellington",
)
head.set_twitter_card(
card="summary",
site_account="@example",
creator_account="@example",
title="Example",
description="Example",
image="https://example.com/image.png",
image_alt="Example",
)
head.set_opengraph_website(
site_name="Example",
title="Example",
description="Example",
url="https://example.com",
image="https://example.com/image.png",
image_alt="Example",
locale="en_US",
)
head.set_favicon(
ico_icon_href="/favicon.ico",
png_icon_16_href="/favicon-16x16.png",
png_icon_32_href="/favicon-32x32.png",
png_icon_64_href="/favicon-64x64.png",
png_icon_96_href="/favicon-96x96.png",
png_icon_180_href="/favicon-180x180.png",
png_icon_196_href="/favicon-196x196.png",
png_apple_touch_icon_57_href="/apple-touch-icon-57x57.png",
png_apple_touch_icon_60_href="/apple-touch-icon-60x60.png",
png_apple_touch_icon_72_href="/apple-touch-icon-72x72.png",
png_apple_touch_icon_76_href="/apple-touch-icon-76x76.png",
png_apple_touch_icon_114_href="/apple-touch-icon-114x114.png",
png_apple_touch_icon_120_href="/apple-touch-icon-120x120.png",
png_apple_touch_icon_144_href="/apple-touch-icon-144x144.png",
png_apple_touch_icon_152_href="/apple-touch-icon-152x152.png",
png_apple_touch_icon_167_href="/apple-touch-icon-167x167.png",
png_apple_touch_icon_180_href="/apple-touch-icon-180x180.png",
png_mstile_70_href="/mstile-70x70.png",
png_mstile_270_href="/mstile-270x270.png",
png_mstile_310x150_href="/mstile-310x150.png",
png_mstile_310_href="/mstile-310x150.png",
)head.set_link_tag("canonical", "https://example.com")
return render_template("index.html", head=head)
return app
````index.html`:
```html
{{ head.top_level_tags }}
{{ head.title }}
{{ head.meta_tags }}
{{ head.link_tags }}Flask App
Right-Click view source
```
or
```html
{{ head() }}
Flask App
Right-Click view source
```
Results in:
```html
Hello World
Flask App
Right-Click view source
```
## Advanced use cases:
### Flask + g
You can use the `g` object in Flask to store the head object.
```python
from flask import Flask, render_template, gfrom pyhead import Head
def create_app():
app = Flask(__name__)@app.before_request
def inject_pyhead():
g.head = Head()
g.head.set_title("My Cool Site")@app.route("/")
def index():
render_template("index.html")
````index.html`
```html
{{ g.head.append_title("Home Page", " - ", _from_template=True) }}
{{ g.head() }}{% block content %}
{% endblock %}
```
Results in:
```html
...
My Cool Site - Home Page...
```### Working with extended templates
The head object can be modified in templates that extend other templates. Here's an example:
`extends.html`
```html
{%- block head -%}
{{ g.head() }}
{% endblock %}{% block content %}
{% endblock %}
```
`index.hml`
```html
{% extends "extends.html" %}
{% block head %}
{{ g.head.append_title('Flask App', ' - ', _from_template=True) }}
{{ super() }}
{% endblock %}{% block content %}
Flask App
Right-Click view source
{% endblock %}```
In this example the ``
tag is removed, and the title is appended, resulting in:```html
...My Cool Site - Flask App
...
```### dict as args, and JSON
You can pass a dict to the Head() constructor to set the values of the tags.
The keys must match the arguments of the set method.```python
head = Head(
...,
twitter_card={
"card": "summary",
"site_account": "@example",
"creator_account": "@example",
"title": "Example",
"description": "Example",
"image": "https://example.com/image.png",
"image_alt": "Example",
},
)
```This is replacing the `set_twitter_card()` method.
With this, you can store the values in a database as JSON objects then pass them to the Head() constructor.
```python
page = model.get_page_by_name("index")head = Head(
...,
twitter=page.twitter_card,
)
```A really efficient way would be to store the entire head object as JSON and pass it to the Head() constructor.
```python
page = model.get_page_by_name("index")head = Head(**page.head)
```the stored JSON data in this case would look something like:
```json
{
"base": "https://example.com",
"title": "Hello World",
"exclude_title_tags": true,
"description": "This is a test",
"keywords": "test, hello, world",
"subject": "Hello World",
"rating": "General",
"robots": "index, follow",
"referrer_policy": {
"policy": "no-referrer",
"fallback": "origin"
},
"google": {
"googlebot": "index, follow",
"no_sitelinks_search_box": true,
"no_translate": true
},
"twitter_card": {
"card": "summary",
"site_account": "@example",
"creator_account": "@example",
"title": "Example",
"description": "Example",
"image": "https://example.com/image.png",
"image_alt": "Example"
},
"favicon": {
"ico_icon_href": "https://example.com/favicon.ico"
}
}
```## CLI Commands
### Generating favicons
You can generate favicons from a source image using the cli command `pyhead favicons -s favicon-gen-test.png`
This uses the python package `favicons` to generate the favicons.
You need to install the `favicons` package to use this command.
```bash
pip install favicons
```All paths in the cli command are relative to the current working directory.
Only the following source formats are supported:
png, jpg, jpeg, gif, svg, tiff
`-s, --source` This will look for the image file to use
`-o, --output` This will be the output directory for the favicons
`-hp, --href-prefix` This will prefix the href tag in the output html
The following command:
```bash
pyhead favicons -s favicon-gen-test.png -o favicons -hp https://example.com
```Will create a folder called `favicons` with the following files:
```text
apple-touch-icon-57x57.png
apple-touch-icon-60x60.png
apple-touch-icon-72x72.png
apple-touch-icon-76x76.png
apple-touch-icon-114x114.png
apple-touch-icon-120x120.png
apple-touch-icon-144x144.png
apple-touch-icon-152x152.png
apple-touch-icon-167x167.png
apple-touch-icon-180x180.png
favicon-16x16.png
favicon-32x32.png
favicon-64x64.png
favicon-96x96.png
favicon-180x180.png
favicon-196x196.png
favicon-delete_me_after_use.html
favicon-delete_me_after_use.py
mstile-70x70.png
mstile-270x270.png
mstile-310x150.png
mstile-310x310.png
```The `favicon-delete_me_after_use.html` file will contain the following:
```html
```
The `favicon-delete_me_after_use.py` file will contain the following:
```python
from pyhead import Headhead = Head()
head.set_favicon(
ico_icon_href="/favicon.ico",
png_icon_16_href="/favicon-16x16.png",
png_icon_32_href="/favicon-32x32.png",
png_icon_64_href="/favicon-64x64.png",
png_icon_96_href="/favicon-96x96.png",
png_icon_180_href="/favicon-180x180.png",
png_icon_196_href="/favicon-196x196.png",
png_apple_touch_icon_57_href="/apple-touch-icon-57x57.png",
png_apple_touch_icon_60_href="/apple-touch-icon-60x60.png",
png_apple_touch_icon_72_href="/apple-touch-icon-72x72.png",
png_apple_touch_icon_76_href="/apple-touch-icon-76x76.png",
png_apple_touch_icon_114_href="/apple-touch-icon-114x114.png",
png_apple_touch_icon_120_href="/apple-touch-icon-120x120.png",
png_apple_touch_icon_144_href="/apple-touch-icon-144x144.png",
png_apple_touch_icon_152_href="/apple-touch-icon-152x152.png",
png_apple_touch_icon_167_href="/apple-touch-icon-167x167.png",
png_apple_touch_icon_180_href="/apple-touch-icon-180x180.png",
png_mstile_70_href="/mstile-70x70.png",
png_mstile_270_href="/mstile-270x270.png",
png_mstile_310x150_href="/mstile-310x150.png",
png_mstile_310_href="/mstile-310x150.png",
)
```Remember to delete the `favicon-delete_me_after_use.html` and `favicon-delete_me_after_use.py` files after use.