# Django Expressions
Django template tag and filter for evaluating Python expressions.
## Demo
As a template tag `{% expr "<expression>" %}`
```django
{% expr "[a for a in range(n)]" %}
```
Save the result to a variable using `as var` at the end of the tag:
```django
{% expr "[a for a in range(n)]" as result %}
{{ result }}
```
As a filter `"<expression>" | expr`:
```django
{{ "{k: v.name for k, v in employees_dict.values() }" | expr }}
```
To chain an expression after another filter, define a lambda expression
as the filter argument `value | expr:"lambda x: <expression>"`:
```django
{{ employees_list | first | expr:"lambda x: x.first_name + ' ' + x.last_name" }}
```
## Installation
1. Install dependencies
```sh
pip install django_expr
```
2. Add `django_expr` to `INSTALLED_APPS` in your `settings.py`
```py
INSTALLED_APPS = [
"django_expr",
...
]
```
3. Inside templates, load the tags with `{% load expr %}`:
```django
{% load expr %}
{% expr "[{k: not v} for k, v in ({'a': 'b', 'c': 0}).items()]" %}
```
4. If you want to automatically include the `expr` tag and filter, add the tags as a 'builtin' in `settings.py`:
```python
TEMPLATES = [
{
...,
'OPTIONS': {
'context_processors': [
...
],
'builtins': [
'django_expr.templatetags.expr',
]
},
},
]
```
Raw data
{
"_id": null,
"home_page": null,
"name": "django-expr",
"maintainer": null,
"docs_url": null,
"requires_python": "<4.0,>=3.8",
"maintainer_email": null,
"keywords": "django, python, expression, django tag, django filter",
"author": null,
"author_email": "Juro Oravec <juraj.oravec.josefson@gmail.com>",
"download_url": "https://files.pythonhosted.org/packages/56/fd/369d9cd58bba3f51d7cabb544bb05ec6d1a56460d472ab3f685ea94eb50c/django_expr-0.1.0.tar.gz",
"platform": null,
"description": "# Django Expressions\n\nDjango template tag and filter for evaluating Python expressions.\n\n## Demo\n\nAs a template tag `{% expr \"<expression>\" %}`\n\n```django\n{% expr \"[a for a in range(n)]\" %}\n```\n\nSave the result to a variable using `as var` at the end of the tag:\n```django\n{% expr \"[a for a in range(n)]\" as result %}\n{{ result }}\n```\n\nAs a filter `\"<expression>\" | expr`:\n\n```django\n{{ \"{k: v.name for k, v in employees_dict.values() }\" | expr }}\n```\n\nTo chain an expression after another filter, define a lambda expression\nas the filter argument `value | expr:\"lambda x: <expression>\"`:\n\n```django\n{{ employees_list | first | expr:\"lambda x: x.first_name + ' ' + x.last_name\" }}\n```\n\n## Installation\n\n1. Install dependencies\n\n ```sh\n pip install django_expr\n ```\n\n2. Add `django_expr` to `INSTALLED_APPS` in your `settings.py`\n\n ```py\n INSTALLED_APPS = [\n \"django_expr\",\n ...\n ]\n ```\n\n3. Inside templates, load the tags with `{% load expr %}`:\n\n ```django\n {% load expr %}\n {% expr \"[{k: not v} for k, v in ({'a': 'b', 'c': 0}).items()]\" %}\n ```\n\n4. If you want to automatically include the `expr` tag and filter, add the tags as a 'builtin' in `settings.py`:\n\n```python\nTEMPLATES = [\n {\n ...,\n 'OPTIONS': {\n 'context_processors': [\n ...\n ],\n 'builtins': [\n 'django_expr.templatetags.expr',\n ]\n },\n },\n]\n```\n",
"bugtrack_url": null,
"license": "MIT",
"summary": "Django template tag and filter for evaluating Python expressions",
"version": "0.1.0",
"project_urls": {
"Homepage": "https://github.com/JuroOravec/alpinui/blob/main/packages/django-expr"
},
"split_keywords": [
"django",
" python",
" expression",
" django tag",
" django filter"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "b9038950d147bf245c1e62bb82c5c274c7c557de2f47b63ee4b97fa019a6369b",
"md5": "011f9c2639ed0dd6b38167ca844b67b5",
"sha256": "577eccdc7da9f69469d35e54e6c03244b926d2f6caa012ce5d4313b1b0ffb676"
},
"downloads": -1,
"filename": "django_expr-0.1.0-py3-none-any.whl",
"has_sig": false,
"md5_digest": "011f9c2639ed0dd6b38167ca844b67b5",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": "<4.0,>=3.8",
"size": 7464,
"upload_time": "2024-08-19T18:04:52",
"upload_time_iso_8601": "2024-08-19T18:04:52.361122Z",
"url": "https://files.pythonhosted.org/packages/b9/03/8950d147bf245c1e62bb82c5c274c7c557de2f47b63ee4b97fa019a6369b/django_expr-0.1.0-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "56fd369d9cd58bba3f51d7cabb544bb05ec6d1a56460d472ab3f685ea94eb50c",
"md5": "aeb54da02c52b029b4e15442d3ac2d19",
"sha256": "c6800e83edfa6afb420e59e1cda84a051a493a5ed511da25e6b48b7dc8a1bd55"
},
"downloads": -1,
"filename": "django_expr-0.1.0.tar.gz",
"has_sig": false,
"md5_digest": "aeb54da02c52b029b4e15442d3ac2d19",
"packagetype": "sdist",
"python_version": "source",
"requires_python": "<4.0,>=3.8",
"size": 7857,
"upload_time": "2024-08-19T18:04:54",
"upload_time_iso_8601": "2024-08-19T18:04:54.838604Z",
"url": "https://files.pythonhosted.org/packages/56/fd/369d9cd58bba3f51d7cabb544bb05ec6d1a56460d472ab3f685ea94eb50c/django_expr-0.1.0.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-08-19 18:04:54",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "JuroOravec",
"github_project": "alpinui",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"lcname": "django-expr"
}