Skip to main content

No project description provided

Project description

head-context

Easily manage your assets in meta tags (scripts, css, preload etc.) from anywhere in the template code (and outside).

Why

Imagine a form widget, which requires a heavy image processing library that we want to include ONLY IF the widget itself was rendered. Thanks to head-context you can specify what resources you need locally (in template fragments, widgets and so on) yet load them in the head section of your page with ease.

What does it do?

<!doctype html>
<html>
<head>
    <title>My Title!</title>
    <!-- this is where we want all our js/css rendered to be rendered -->
    {{ head_placeholder() }}
</head>
<body>
    {% include "my-cool-component.html" %}
</body>
</html>

And my-cool-component.html:

<!-- we can call these from anywhere and they will be automatically rendered in the right place! -->
{% do push_js('/static/cool-component.js', mode="async") %}
{% do push_css('/static/cool-component.css') %}
{% do push_preload('/static/some-image-we-need.png', 'image') %}
<div class="my-cool-component">
    <!-- ... --->
</div>

And that's pretty much it. You can push_js/push_css/push_preload from anywhere in the template (and even outside of templates) and it will be automatically attached to the page being rendered.

Features

  • Supports scripts, styles and preload directives
  • Works with Jinja2
  • Can be used from Python code too
    • simply use head_context.push_js/push_css/push_preload from Python code
    • it needs to run during template rendering though (otherwise it wouldn't make sense)
    • useful if you have form widget rendering written in Python for example
    • or basically any kind of rendering written in Python

Installation and setup

Simply install head-context package:

pip install head-context
# or with poetry
poetry add head-context

Add extension to the Jinja2 environment:

from jinja2 import Environment

env = Environment()
env.add_extension("head_context.jinja_ext.HeadContextExtension")

and that's it! From now on you can use push_css()/push_js()/push_preload() and head_placeholder().

Usage with Flask

To use this extension with Flask simply add it when configuring the app:

def create_app():
    app = Flask("app", __name__)
    app.jinja_env.add_extension("head_context.jinja_ext.HeadContextExtension")
    app.jinja_env.add_extension("jinja2.ext.do")
    
    return app

FAQ

Does this work with asyncio?

head-context uses contextvars under the hood, which are compatible with asyncio but it integrates with Jinja in a way that won't work with templates which use asyncio rendering. If you have any good ideas how to make it work a PR would be welcome.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

head_context-0.1.1.tar.gz (5.6 kB view details)

Uploaded Source

Built Distribution

head_context-0.1.1-py3-none-any.whl (6.0 kB view details)

Uploaded Python 3

File details

Details for the file head_context-0.1.1.tar.gz.

File metadata

  • Download URL: head_context-0.1.1.tar.gz
  • Upload date:
  • Size: 5.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.3.1 CPython/3.11.1 Darwin/22.1.0

File hashes

Hashes for head_context-0.1.1.tar.gz
Algorithm Hash digest
SHA256 e0b608313761e917c0b05a0a0a600a9aed59bdef0086d1f97b3fdbac1991f7fc
MD5 f3b006363308c086b4485c20ca10ab34
BLAKE2b-256 4ec8cf7a0bf798bbff63c6d257df756ff03d80b6f38e6590f6489efb51ab610a

See more details on using hashes here.

File details

Details for the file head_context-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: head_context-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 6.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.3.1 CPython/3.11.1 Darwin/22.1.0

File hashes

Hashes for head_context-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 a71b4b745f596e52eeb89a2f578854c99c3ffc266f5c2bf7d1ea3a0d079aca35
MD5 d78b4fdfed62a1b991ff83ac2ef9a47c
BLAKE2b-256 542ab0bc8259aa92e8cba0c82e7bfae20605f78328b470910ec35c8f91ee03aa

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page