Skip to content

Error with Jinja2 3.1.5 and new Plotly 6.0.0 #5055

Open
@bsense-rius

Description

@bsense-rius

Dear community,

Scope

Since few days ago I had a notebook in sagemaker studio (with sagemaker-distribution 2.2.2) that ran smoothly without issues with Jinja2 3.1.5 and Plotly 5.24.1 that used plotly's to_html().

17 days ago after an update of sagemaker distribution 2.2.2, plotly 5.24.1 was upgraded to plotly 6.0,0, amongst several other libaries. Our code stopped working. We ensured that the issue was with plotly 6.0.0 by going to our google colab instance that had plotly 5.24.1 with our code also runnining without issues. BUT when we installed package plotly 6.0.0 it stopped working with the very same error.

Error details

We use a Jinja2 template with several placeholders for plotly charts that are updated in a while loop. Only the first iteration over the template works, the second is failing with the following error.

---------------------------------------------------------------------------
TemplateSyntaxError                       Traceback (most recent call last)
Cell In[15], line 809
    806             with open(htmlFilename, 'w') as f:
    807                 f.write(htmlTemplate)
--> 809 analyzeSKF()
    811 # Clear memory
    812 import gc

Cell In[15], line 651, in analyzeSKF()
    644 if wdgChartHTML.value == True:
   (...)
--> 651     htmlTemplate = htmlEnvAdd.from_string(htmlTemplate)
    652     htmlTemplate = htmlTemplate.render({f"figTimeChart{htmlTimeChartIdx}": fig.to_html(full_html=False)})
    653     htmlTimeChartIdx = htmlTimeChartIdx + 1

File /opt/conda/lib/python3.11/site-packages/jinja2/environment.py:1111, in Environment.from_string(self, source, globals, template_class)
   1109 gs = self.make_globals(globals)
   1110 cls = template_class or self.template_class
-> 1111 return cls.from_code(self, self.compile(source), gs, None)

File /opt/conda/lib/python3.11/site-packages/jinja2/environment.py:771, in Environment.compile(self, source, name, filename, raw, defer_init)
    769     return self._compile(source, filename)
    770 except TemplateSyntaxError:
--> 771     self.handle_exception(source=source_hint)

File /opt/conda/lib/python3.11/site-packages/jinja2/environment.py:942, in Environment.handle_exception(self, source)
    937 """Exception handling helper.  This is used internally to either raise
    938 rewritten exceptions or return a rendered traceback for the template.
    939 """
    940 from .debug import rewrite_traceback_stack
--> 942 raise rewrite_traceback_stack(source=source)

File <unknown>:2015, in template()

TemplateSyntaxError: expected token 'end of print statement', got 'q'

As you can see below the code is pretty straight forward: We init the Jinja2 Environment objects and in the main body we update the htmlTemplate with plotly's figure to_html(),

First iteration is correct, but next one just returns the from_string() error shown above.


from jinja2 import Environment, Template, DebugUndefined, Undefined, StrictUndefined, FileSystemLoader, select_autoescape

# Initializes globals (not best practice)
def reloadHTMLTemplate():
    global htmlTemplate, htmlEnvAdd, htmlEnvFinish

    # Load the template from file
    htmlEnvAdd    = Environment(trim_blocks=True, lstrip_blocks=True, undefined=DebugUndefined)
    htmlEnvFinish = Environment(trim_blocks=True, lstrip_blocks=True, undefined=Undefined)

    # The {% %} are evaluated just once, so they are ALL processed in first render
    htmlTemplate = u'''\
    <!DOCTYPE html>
    <html>
      <head>
        <meta charset="utf-8" />   <!--It is necessary to use the UTF-8 encoding with plotly graphics to get e.g. negative signs to render correctly -->
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
      </head>
      <body>
        <h2 style="font-family:'Open Sans', verdana, arial, sans-serif; color: #2A3F5F">{{ reportTitle }}</h2>
        <h3 style="font-family:'Open Sans', verdana, arial, sans-serif; color: #2A3F5F">{{ titleTimeCharts }}</h3>
        {{ figTimeChart0 }}
        {{ figTimeChart1 }}
        {{ figTimeChart2 }}
        {{ figTimeChart3 }}

        <h3 style="font-family:'Open Sans', verdana, arial, sans-serif; color: #2A3F5F">{{ titleFreqCharts }}</h3>
        {{ figFreqChart0 }}
        {{ figFreqChart1 }}
        {{ figFreqChart2 }}
        {{ figFreqChart3 }}
      </body>
    </html>'''

##############################################
# Main function point that uses the Jinja2 variables 
##############################################
reloadHTMLTemplate()
htmlTimeChartIdx = 0
(...)
while htmlTimeChartIdx < 4:
    # create the figure 'fig' with plotly
    (...)
    if wdgChartHTML.value == True:
        htmlTemplate = htmlEnvAdd.from_string(htmlTemplate)
        htmlTemplate = htmlTemplate.render({f"figTimeChart{htmlTimeChartIdx}": fig.to_html(full_html=False)})
        htmlTimeChartIdx = htmlTimeChartIdx + 1

Conclusion

It seems that there might be a kind of change in the to_html() or in the figure object itself that fools Jinja2's from_sting in the next loop iteration.

Thank you very much!

Metadata

Metadata

Assignees

No one assigned

    Labels

    P3backlogbugsomething broken

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions