Skip to main content

Simple theme mechanism for Flask

Project description

PyPI PyPI - License GitHub Workflow Status

Flask-Themer

Simple theme support for flask apps.

Flask-Themer is inspired by the (seemingly) abandoned flask-themes project, but has been written from scratch for py3.7+ (or 3.6 with the dataclasses backport). However it is not immediately compatible with flask-themes and does not seek to be. Flask-Themer tries to have little opinion on how you actually structure your project and its themes and does not require a particular metadata format/file.

Flask-Themer releases follow Semantic Versioning. Flask-Themer has 100% test coverage and considers it an error to fall below 100%.

Installation

Install the latest release from PyPi:

pip install flask-themer

or get the latest development version from github:

git clone https://github.com/TkTech/flask-themer.git
cd flask-themer
python setup.py develop

Quickstart

Flask-Themer usage is usually very basic, and once setup you likely won't need to touch it again. Lets do a quickstart. Notice how we import render_template from flask_themer instead of flask.

Our app.py looks like this:

from flask import Flask
from flask_themer import Themer, render_template

app = Flask(__name__)
themer = Themer(app)


@themer.current_theme_loader
def get_current_theme():
    # This is where you would look up the current user's theme if one was
    # logged in, for example.
    return 'default'

@app.route('/')
def hello_world():
    return render_template('hello.html')

And next to it we have a directory called themes with a directory called default inside of it. Our themes/default/hello.html looks like this:

Hello world!

That's it! By default Flask-Themer will look for a themes directory next to your project and assume all the directories inside of it are themes. You can change what directory it looks for with THEMER_DEFAULT_DIRECTORY, or specify the template loaders explicitly to overwrite the default:

from flask_themer import Themer, FileSystemThemeLoader

app = Flask(__name__)
themer = Themer(app, loaders=[
    FileSystemThemeLoader(app, os.path.join(
        app.root_path,
        'also_themes'
    ))
])

Using Themes From Templates

Two template globals are added once Flask-Themer is setup, theme() and theme_static() (just like flask-themes). These methods look up the currently active theme and look for the given path in that theme, returning a special path that Jinja can use to load it.

{% extends theme("base.html") %}

{% block header %}
    {{ super() }}
    <link rel="stylesheet" href="{{ theme_static("bootstrap.css") }}">
{% endblock %}

Theme Loaders

Theme loaders are the mechanism by which Flask-Themer discovers what themes are available. You can create a custom loader to get themes from a ZIP file, or a database for example. Usually if you create a new ThemeLoader you'll also need to create a new Jinja template loader so Jinja knows how to read individual templates. Lets do a very minimal example that loads just a single theme from a ZIP file.

from zipfile import ZipFile
from flask_themer import ThemeLoader, Theme
from jinja2.loaders import BaseLoader, TemplateNotFound

class ZipFileTemplateLoader(BaseLoader):
    def __init__(self, *args, archive, **kwargs):
        super().__init__(*args, **kwargs)
        self.archive = archive

    def get_source(self, environment, template):
        try:
            return (self.archive.read(template), None, False)
        except KeyError:
            raise TemplateNotFound(template)


class ZipFileThemeLoader(ThemeLoader):
    def __init__(self, path_to_zip):
        self.archive = ZipFile(path_to_zip)

    @property
    def themes(self):
        yield Theme(
            name='my_dumb_theme',
            theme_loader=self,
            jinja_loader=ZipFileTemplateLoader(archive=self.archive),
        )

    def get_static(self, theme, path):
        return self.archive.read(path)

And then to use our new loaders we update our previous example:

...
themer = Themer(app, loaders=[
    ZipFileThemeLoader('my_dumb_theme.zip')
])
...

Pretty simple right? You can see how we could easily create a loader to load multiple themes from an archive, or load a user's customized theme from a database.

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

flask-themer-1.3.0.tar.gz (5.9 kB view details)

Uploaded Source

Built Distributions

flask_themer-1.3.0-py3.7.egg (9.7 kB view details)

Uploaded Source

flask_themer-1.3.0-py3-none-any.whl (6.8 kB view details)

Uploaded Python 3

File details

Details for the file flask-themer-1.3.0.tar.gz.

File metadata

  • Download URL: flask-themer-1.3.0.tar.gz
  • Upload date:
  • Size: 5.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.41.1 CPython/3.7.5

File hashes

Hashes for flask-themer-1.3.0.tar.gz
Algorithm Hash digest
SHA256 327db7e3cc2cd952e0dab9f57ebf4d7f4e90688a53a709e407d6c363f0f239d1
MD5 de953c13e8bc2c85db9cf71f729a0082
BLAKE2b-256 ebaedb74d350938165cc706092c941fb603312d177050b349a9962a177a9eed3

See more details on using hashes here.

File details

Details for the file flask_themer-1.3.0-py3.7.egg.

File metadata

  • Download URL: flask_themer-1.3.0-py3.7.egg
  • Upload date:
  • Size: 9.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.41.1 CPython/3.7.5

File hashes

Hashes for flask_themer-1.3.0-py3.7.egg
Algorithm Hash digest
SHA256 ae3c0a3f17d2ee81bae573e0f34424fa9f8172f5d4311fec27526ab1035e2575
MD5 45a41a41b87098cebcc11b01dd56b2f9
BLAKE2b-256 b57f8aa9cdcdc2ed5d01da360ce60f7dc65849bca489129f277f5232494b853d

See more details on using hashes here.

File details

Details for the file flask_themer-1.3.0-py3-none-any.whl.

File metadata

  • Download URL: flask_themer-1.3.0-py3-none-any.whl
  • Upload date:
  • Size: 6.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.41.1 CPython/3.7.5

File hashes

Hashes for flask_themer-1.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 ddfc17c7ddf2de1a6292c09f7bc9b0953c5c786a3320b02c4842ebb58418323e
MD5 a3f2b6d0bdc243b5ca718a2c7a97be98
BLAKE2b-256 fc6847e86ba5a6c3d404d7b84ab274c0209f449b31c8a38ef484a0725e3c7beb

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