Within the template, no, you cannot import python code.
The way to do this is to register the function as a jinja2 custom filter, like this:
In your python file:
from dates.format import timesince
environment = jinja2.Environment(whatever)
environment.filters['timesince'] = timesince
# render template here
In your template:
{% macro time(mytime) %}
<a title="{{ mytime }}">{{ mytime|timesince }}</a>
{% endmacro %}
Related Contents:
- How do I import other Python files?
- How do I unload (reload) a Python module?
- How to import a module given its name as string?
- Python error “ImportError: No module named”
- Use ‘import module’ or ‘from module import’?
- ModuleNotFoundError: What does it mean __main__ is not a package?
- Why is Python running my module when I import it, and how do I stop it?
- What’s the correct way to sort Python `import x` and `from x import y` statements?
- How do I format a date in Jinja2?
- In Python, what happens when you import inside of a function? [duplicate]
- django import error – No module named core.management
- Check if Python Package is installed
- Should I use `import os.path` or `import os`?
- When to use os.name, sys.platform, or platform.system?
- Python Nose Import Error
- Python: reload component Y imported with ‘from X import Y’?
- Link to Flask static files with url_for
- Import file from parent directory?
- How to increment a variable on a for loop in jinja template?
- Jinja2 inline comments
- Absolute vs. explicit relative import of Python module
- How do I render jinja2 output to a file in Python instead of a Browser
- ImportError: No module named mock
- Is there an idiomatic file extension for Jinja templates?
- How to convert string to uppercase / lowercase in Jinja2?
- How to test or mock “if __name__ == ‘__main__'” contents
- Import order coding standard
- How can I import a module dynamically given its name as string?
- Check if key exists in a Python dict in Jinja2 templates
- Jinja2 template not rendering if-elif-else statement properly
- What are good rules of thumb for Python imports?
- Length of string in Jinja/Flask
- What exactly does “import *” import?
- range in jinja2 inside a for loop
- How to use g.user global in flask
- Dynamic loading of python modules [duplicate]
- How to dynamically select template directory to be used in flask?
- Local import statements in Python
- Using sass with Flask and jinja2
- jinja2.exceptions.TemplateNotFound error [duplicate]
- Is it possible to use AngularJS with the Jinja2 template engine?
- Importing module from string variable using “__import__” gives different results than a normal import statement
- zip(list1, list2) in Jinja2?
- Is there a direct approach to format numbers in jinja2?
- Import statement works on PyCharm but not from terminal
- Is “from matplotlib import pyplot as plt” == “import matplotlib.pyplot as plt”?
- What does this “-” in jinja2 template engine do?
- What is the process to create pdf reports with charts from a DB?
- Mako or Jinja2? [closed]
- How to import .py file from another directory? [duplicate]