Skip to content

Commit

Permalink
Lazy load WeasyPrint
Browse files Browse the repository at this point in the history
It gives an interesting boost performance that’s very useful when
auto-reloading is enabled.

Related to Kozea/WeasyPrint#2073.
  • Loading branch information
liZe committed Feb 17, 2024
1 parent fb9f343 commit 6604003
Showing 1 changed file with 9 additions and 5 deletions.
14 changes: 9 additions & 5 deletions flask_weasyprint/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

from urllib.parse import urljoin, urlsplit

import weasyprint
from flask import current_app, has_request_context, request
from werkzeug.test import Client, ClientRedirectError, EnvironBuilder
from werkzeug.wrappers import Response
Expand Down Expand Up @@ -71,8 +70,7 @@ def dispatch(url_string):
return dispatch


def make_url_fetcher(dispatcher=None,
next_fetcher=weasyprint.default_url_fetcher):
def make_url_fetcher(dispatcher=None, next_fetcher=True):
"""Return an function suitable as a ``url_fetcher`` in WeasyPrint.
You generally don’t need to call this directly.
Expand All @@ -92,6 +90,10 @@ def make_url_fetcher(dispatcher=None,
Typically ``base_url + path`` is equivalent to the passed URL.
"""
if next_fetcher is True:
from weasyprint import default_url_fetcher # lazy loading
next_fetcher = default_url_fetcher

if dispatcher is None:
dispatcher = make_flask_url_dispatcher()

Expand Down Expand Up @@ -157,11 +159,13 @@ def HTML(*args, **kwargs):
This requires a Flask :doc:`request context <flask:reqcontext>`.
"""
return _wrapper(weasyprint.HTML, *args, **kwargs)
from weasyprint import HTML # lazy loading
return _wrapper(HTML, *args, **kwargs)


def CSS(*args, **kwargs):
return _wrapper(weasyprint.CSS, *args, **kwargs)
from weasyprint import CSS # lazy loading
return _wrapper(CSS, *args, **kwargs)


CSS.__doc__ = HTML.__doc__.replace('HTML', 'CSS')
Expand Down

0 comments on commit 6604003

Please sign in to comment.