Error with Jinja2 3.1.5 and new Plotly 6.0.0
还没有人认领这个 Issue。
- 主要语言
- Python
- 星标
- 18.8k
- 派生
- 2.8k
- 平均合并
- 16 小时 26 分钟
- 30 天内合并 PR
- 21
描述
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!
贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
调研方向
使用 Jinja2 3.1.5 以及 Plotly 5.24.1 和 6.0.0 重现所提供的循环,重点关注 Environment.from_string()、render() 和 fig.to_html()。比较第二次迭代之前的模板和生成的 HTML;当重复插入图表不再引发 TemplateSyntaxError 时即表示完成。
由索引模型根据 Issue 内容生成。
评估
- 技术栈
- jupyter-notebook, plotly, python
- 领域
- data-visualization
- Issue 类型
- 缺陷
- 难度
- 4/5
- 预计耗时
- 3-5 天
- 活跃度
- 活跃
- 描述清晰度
- 基本清楚
- 新手友好度
- 45/100