Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Rendertron middleware for python applications.

License

NotificationsYou must be signed in to change notification settings

frontendr/python-rendertron

Repository files navigation

Rendertron middleware for Python applications.

Build StatusCoverage StatusCode style: black

Rendertron is a headless Chromerendering solution designed to render & serialise web pages on the fly. Thegoal of this package is to provide middleware to render a request using aRendertron service and make the result available.

This makes it possible to for example render Progressive Web Apps (PWA), waitfor it to fully render (completes initial data loading etc.) and use thatfully built markup as a response.

Besides the fact that your user will see a fully rendered application faster italso allows search engines to properly index the markup.

Installing

Install a Rendertron service by following the steps inthe documentation.

Install this package usingpip:

pip install rendertron

You can also install the latest development version usingpip's-e flag:

pip install -e git://git@github.com:frontendr/python-rendertron.git@develop#egg=rendertron

This will install thedevelop branch.

Django

First, add'rendertron' to theINSTALLED_APPS list in settings.

Then you have 2 choices:

  • Enable themiddleware and render everything that matches eitherRENDERTRON_INCLUDE_PATTERNS or does not matches anything inRENDERTRON_EXCLUDE_PATTERNS. See the Configuration section for more information aboutthose.
  • Decorate specific views with the@rendertron_render decorator to only let renderthose views with the Rendertron service.

Middleware

  1. Add'rendertron.middleware.DjangoRendertronMiddleware' to theMIDDLEWARElist in the settings.
  2. Make sure to specify eitherRENDERTRON_INCLUDE_PATTERNS to specify path patternswhich are to be rendered by the Rendertron service orRENDERTRON_EXCLUDE_PATTERNS_EXTRAto only specify what to exclude.

Decorate specific views

Instead of relying on the middleware and settings it is also possible to decoratespecific views with the@rendertron_render decorator.

fromrendertron.decorators.djangoimportrendertron_render@rendertron_renderdefmy_view(request):    ...

For class based views use Django'smethod_decorator.

fromdjango.utils.decoratorsimportmethod_decoratorfromdjango.views.genericimportTemplateViewfromrendertron.decorators.djangoimportrendertron_render@method_decorator(rendertron_render,name="dispatch")classMyView(TemplateView):    ...

Configuration

Most configuration is done by specifying specific variables. For Django usersthat's done in your site's settings.

The following settings are available:

SettingDefaultDescription
RENDERTRON_BASE_URL'http://localhost:3000/'The url the Rendertron service is listening on.
RENDERTRON_RENDER_QUERY_PARAM'rendertron_render'The query parameter added to the request url passed to Rendertron. This is used to differentiate normal requests with requests from Rendertron.
RENDERTRON_STORAGESee StorageAn object literal specifying and configuring the storage class to be used. See the Storage section for more information.
RENDERTRON_INCLUDE_PATTERNS[]A list of regular expression patterns to include. Once a pattern in this list matches the request no further checking will be done.
RENDERTRON_EXCLUDE_PATTERNSList of common extensions.By default this is a list of common static file type extensions used on the web. If Django is detected it'sSTATIC_URL andMEDIA_URL paths are added to the list. Note that if you override this setting all defaults are gone. If you want to keep these defaultsand add your own patterns useRENDERTRON_EXCLUDE_PATTERNS_EXTRA.
RENDERTRON_EXCLUDE_PATTERNS_EXTRA[]LikeRENDERTRON_EXCLUDE_PATTERNS but will be appended to that list.

Storage

Storage classes are handling the logic of storing the results coming from theRendertron service for a period of time. They handle if, how, where and howlong a result is stored. There are some core storage classes available thesystem is built for it to be very easy to built your own.

The choice of one of the built in storage classes depends on your framework.

Any framework:DummyStorage

A storage class that doesn't do anything. It doesn't store and will never returna stored result.

To use it simply setRENDERTRON_STORAGE['CLASS'] to'rendertron.storage.DummyStorage'. It has no options.

Django:DjangoCacheStorage

A storage class that utilizes Django's cache framework to store the results.

To use it simply setRENDERTRON_STORAGE['CLASS'] to'rendertron.storage.DjangoCacheStorage'. It has the following options:

SettingDefaultDescription
TIMEOUTDjango'sDEFAULT_TIMEOUT cache setting which is300 (5 minutes)The number of seconds the result should be stored in the cache. It's thetimeout argument for Django'scache.set method.
VERSIONNoneTheversion argument which is passed to Django'scache.set method.

Example config:

RENDERTRON_STORAGE= {'CLASS':'rendertron.storage.DjangoCacheStorage','OPTIONS': {'TIMEOUT':300,    }}

Running tests

First install Django to be able to test Django related things.

pip install django

Then run the tests viadjango-admin using the provided minimal settings file.

django-admintest --pythonpath. --settings tests.django.settings

License

MIT

About

Rendertron middleware for python applications.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors2

  •  
  •  

[8]ページ先頭

©2009-2025 Movatter.jp