对于jupyter sas_kernel,我正在为nbconvert(版本4.2.0)编写模板。基本布局如下:
{%- extends 'null.tpl' -%}
{# Comment block #}
{% block header %}
/* coding: SAS utf-8 */
{% endblock header %}
{% block in_prompt %}
/* In[{{ cell.execution_count if cell.execution_count else ' ' }}]: */
{% endblock in_prompt %}
{% block input %}
{{ cell.source | ipython2python | sas_magic_regex }}
{% endblock input %}
{% block markdowncell scoped %}
/*
{{ cell.source | comment_lines(prefix='*') }}
*/
{% endblock markdowncell %}
在块输入部分,我需要使用正则表达式来处理来自ipython2python
的文本,主要用于处理魔术调用。我无法从string filers看到正则表达式函数
所以我写了自己的:
def sas_magic_regex(string:str) -> str:
""" Replace Python Magics with a commented out version or the SAS x command """
sas_magic=re.compile(r'^get_ipython.*?(\w+)\(\'(.*)\'\)')
newStr=sas_magic.search(string)
if len(newStr.groups())==2:
if newStr.group(1)=='system':
return 'x ' + newStr.group(2) + ';'
else:
return '/* Jupyter magic ' + string + ' */'
env = Environment()
env.filters['sas_magic_regex'] = sas_magic_regex
问题是nbconvert无法找到这个jinja过滤器。
我收到此错误消息:jinja2.exceptions.TemplateAssertionError: no filter named 'sas_magic_regex'