Disable jinja2 if it is not correctly installed

feature/health-checks
Adi Eyal 2023-05-30 08:53:32 +02:00
parent 53e69feda7
commit e36260151d
3 changed files with 30 additions and 3 deletions

View File

@ -19,6 +19,7 @@ from install import check_correct_dynamicprompts_installed, get_update_command
from sd_dynamic_prompts import __version__, callbacks
from sd_dynamic_prompts.element_ids import make_element_id
from sd_dynamic_prompts.generator_builder import GeneratorBuilder
from sd_dynamic_prompts.health_check import check_jinja_available
from sd_dynamic_prompts.helpers import (
generate_prompts,
get_magicmodels_path,
@ -35,6 +36,8 @@ VERSION = __version__
logger = get_logger(__name__)
logger.info(f"Starting Dynamic Prompts v{VERSION}")
is_jinja_available = check_jinja_available()
check_correct_dynamicprompts_installed()
base_dir = Path(scripts.basedir())
magicprompt_models_path = get_magicmodels_path(base_dir)
@ -260,7 +263,7 @@ class Script(scripts.Script):
with gr.Accordion("Need help?", open=False):
gr.HTML(html)
with gr.Group():
with gr.Group(visible=is_jinja_available):
with gr.Accordion("Jinja2 templates", open=False):
enable_jinja_templates = gr.Checkbox(
label="Enable Jinja2 templates",

View File

@ -12,8 +12,11 @@ from dynamicprompts.generators import (
from dynamicprompts.parser.parse import default_parser_config
from sd_dynamic_prompts.frozenprompt_generator import FrozenPromptGenerator
from sd_dynamic_prompts.health_check import check_jinja_available
from sd_dynamic_prompts.utils import get_logger
is_jinja_available = check_jinja_available()
logger = get_logger(__name__)
@ -88,8 +91,11 @@ class GeneratorBuilder:
return self
def set_is_jinja_template(self, is_jinja_template=True, limit_prompts=False):
self._is_jinja_template = is_jinja_template
self._limit_jinja_prompts = limit_prompts
if is_jinja_available:
self._is_jinja_template = is_jinja_template
self._limit_jinja_prompts = limit_prompts
else:
self._is_jinja_template = False
return self

View File

@ -0,0 +1,18 @@
import logging
from functools import lru_cache
logger = logging.getLogger(__name__)
@lru_cache(maxsize=1)
def check_jinja_available():
try:
from dynamicprompts.generators import JinjaGenerator
_ = JinjaGenerator({}, "")
return True
except Exception as e:
logger.warning(
f"Jinja2 not available: {e}. Please install Jinja2 with the following command: 'pip install -U Jinja2'",
)
return False