Reputation: 66730
A bottle project of mine uses Jinja2. PyCharm does not automatically recognize it and shows such lines as errors. Is there a way to make Jinja2 work?
Upvotes: 89
Views: 54427
Reputation: 1209
If you are using PyCharm community edition, there is no native support for Jinja2 and the only available plugin needs subscription payments.
I could somehow make my .j2
files human readable by doing the following:
+
in the "Recognizable File Types" list, name it Jinja2 and then assign .j2
file types to it.Finally here are the content of four "Keywords" tabs:
1:
attr
autoscape
block
capitalize
class
dict
do
elif
else
endautoscape
endblock
endfilter
endfor
endif
endmacro
endset
eval
extends
filter
for
forceescape
format
if
include
loop
macro
pprint
range
rejectattr
safe
select
selectattr
set
title
2:
abs
callable
default
defined
divisibleby
equalto
escaped
even
first
groupby
iterable
join
last
lower
mapping
odd
random
reverse
sameas
sequence
sort
sum
undefined
upper
3:
bool
false
float
hex
int
null
number
string
true
4:
"{%
"{{
%}
%}"
(expr)
**
+%}
-
-%}
-}}
and
in
is
not
or
random
{%
{%+
{%-
{{
{{-
|
}}
}}"
Upvotes: 3
Reputation: 1
from jinja2 import Template
import pandas as pd
a = "Variable A"
b = "Variable B"
data = {'Column1': [1, 2, 3],'Column2': ['A', 'B', 'C']} df = pd.DataFrame(data)
template_str = """<html><head><title>Data Frame andVariables</title></head><body><h2>{{ variable_a }}</h2><h2>{{ variable_b }}</h2><table border="1"><thead><tr>{% for column in df.columns %}<th>{{ column }}</th>{% endfor %}</tr></thead><tbody>{% for index, row in df.iterrows() %}<tr>{% for value in row %}<td>{{ value }}</td>{% endfor %}</tr>{% endfor %}</tbody></table></body></html>"""
template = Template(template_str)
html_output =template.render(variable_a=a,variable_b=b, df=df)
print(html_output)
Upvotes: -1
Reputation: 2859
If you are using .jinja
extension instead of .jinja2
, it won't work, templates are not highlighted.
You have to add the file extension to the filetypes section.
*.jinja
Upvotes: 26
Reputation: 1
In community edition, the python template option is not available, so you can simply click on python packages next to the terminal present on the bottom. This will also add Jinja2
Upvotes: 0
Reputation: 66730
In the pro edition, these template languages:
are supported. You can configure the template language in the project's settings:
The community edition may lack certain template languages.
Upvotes: 118
Reputation: 5471
Yes pro edition from pycharm does support Jinja2 to enable it go here
From File open Settings and search for python template under Languages & Frameworks Select Python Template Languages from there Click HTML And Select Jinja2 as Template Language.
please see the image for better understanding.
Upvotes: 41
Reputation: 6285
I think it's worth to mention that PyCharm Community edition does not support Jinja2, Mako and Django. It's available only in PyCharm Professional.
See comparison of the two.
Upvotes: 65