Skip to content

Commit

Permalink
Mako warning & documentation how to use templates (#409)
Browse files Browse the repository at this point in the history
- Warnings against mako
- Document how to use templates
- Remove doc references of the extras
- Update docs related to template usage
  • Loading branch information
devkral authored Oct 2, 2024
1 parent 3478e68 commit 0364c7a
Show file tree
Hide file tree
Showing 8 changed files with 25 additions and 51 deletions.
18 changes: 3 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,12 +81,6 @@ $ pip install uvicorn

If you want install esmerald with specifics:

**Support for template system such as jinja2 and mako**:

```shell
$ pip install esmerald[templates]
```

**Support for the internal scheduler**:

```shell
Expand All @@ -99,12 +93,6 @@ $ pip install esmerald[schedulers]
$ pip install esmerald[jwt]
```

**Support for ORJSON and UJSON**:

```shell
$ pip install esmerald[encoders]
```

**If you want to use the esmerald testing client**:

```shell
Expand Down Expand Up @@ -357,11 +345,11 @@ For a classic, direct, one file single approach.
**In a nutshell**:

```python title='src/app.py'
from esmerald import Esmerald, get, status, Request, UJSONResponse, Gateway, WebSocketGateway, Websocket
from esmerald import Esmerald, get, status, Request, ORJSONResponse, Gateway, WebSocketGateway, Websocket

@get(status_code=status.HTTP_200_OK)
async def home() -> UJSONResponse:
return UJSONResponse({
async def home() -> ORJSONResponse:
return ORJSONResponse({
"detail": "Hello world"
})

Expand Down
16 changes: 9 additions & 7 deletions docs/en/docs/configurations/template.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,8 @@ You are free to build your own and pass it to the `TemplateConfig`. This way you

You will notice the name of the parameters in the `TemplateConfig` match maority of the jinja2 implementation.

## Requirements

This section requires `jinja` or `mako` to be installed. You can do it so by running:

```shell
$ pip install esmerald[templates]
```
!!! Warning
The `Mako` engine has a limited integration within Esmerald. This will change in the future.

## TemplateConfig and application

Expand Down Expand Up @@ -64,3 +59,10 @@ you can do something like this:
```jinja
{!> ../../../docs_src/_shared/jinja.html!}
```

## How to use

Simply return `Template` (of esmerald) not `TemplateResponse` with a `name` parameter pointing to the relative path of the template.
You can pass extra data via setting the `context` parameter to a dictionary containing the extra data.

To select the return type (txt, html) you need to name the files: `foo.html.jinja`.
14 changes: 0 additions & 14 deletions docs/en/docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,14 +80,6 @@ up to you.
$ pip install uvicorn
```

If you want install esmerald with specifics:

**Support for template system such as jinja2 and mako**:

```shell
$ pip install esmerald[templates]
```

**Support for the internal scheduler**:

```shell
Expand All @@ -100,12 +92,6 @@ $ pip install esmerald[schedulers]
$ pip install esmerald[jwt]
```

**Support for ORJSON and UJSON**:

```shell
$ pip install esmerald[encoders]
```

**If you want to use the esmerald testing client**:

```shell
Expand Down
2 changes: 1 addition & 1 deletion docs/en/docs/responses.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ Some responses use extra dependencies, such as [UJSON](#ujson) and [OrJSON](#orj
responses, you need to install:

```shell
$ pip install esmerald[encoders]
$ pip install ujson orjson
```

This will allow you to use the [OrJSON](#orjson) and [UJSON](#ujson) as well as the
Expand Down
5 changes: 0 additions & 5 deletions esmerald/config/template.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,6 @@ class TemplateConfig(BaseModel):
This configuration is a simple set of configurations that when passed enables the template engine.
!!! Note
You might need to install the template engine before
using this. You can always run
`pip install esmerald[templates]` to help you out.
**Example**
```python
Expand Down
5 changes: 4 additions & 1 deletion esmerald/datastructures/encoders.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
UJSONResponse = None # type: ignore


class OrJSON(ResponseContainer[ORJSONResponse]):
class ORJSON(ResponseContainer[ORJSONResponse]):
content: Annotated[
Optional[Dict[str, Any]],
Doc(
Expand Down Expand Up @@ -77,6 +77,9 @@ def to_response(
)


OrJSON = ORJSON


class UJSON(ResponseContainer[UJSONResponse]):
content: Annotated[
Optional[Dict[str, Any]],
Expand Down
6 changes: 3 additions & 3 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -96,17 +96,17 @@ dev = [
"uvicorn[standard]>=0.24.0",
]

templates = ["mako>=1.2.4,<2.0.0"]
jwt = ["passlib==1.7.4", "python-jose>=3.3.0,<4"]
encoders = ["ujson>=5.7.0,<6"]
schedulers = ["asyncz>=0.11.0"]
all = [
"esmerald[test,dev,templates,jwt,encoders,schedulers]",
"esmerald[test,dev,jwt,schedulers]",
"ipython",
"ptpython",
"a2wsgi",
]
testing = [
"mako>=1.2.4,<2.0.0",
"ujson>=5.7.0,<6",
"anyio[trio]>=3.6.2,<5.0.0",
"brotli>=1.0.9,<2.0.0",
"edgy[postgres]>=0.16.0",
Expand Down
10 changes: 5 additions & 5 deletions tests/test_datastructures_response.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

from esmerald import Gateway, get, status
from esmerald.datastructures import JSON
from esmerald.datastructures.encoders import UJSON, OrJSON
from esmerald.datastructures.encoders import ORJSON, UJSON
from esmerald.testclient import create_client


@pytest.mark.parametrize("response", [JSON, OrJSON, UJSON])
@pytest.mark.parametrize("response", [JSON, ORJSON, UJSON])
def test_json_datastructure(response, test_client_factory):
@get()
async def home() -> response:
Expand All @@ -19,7 +19,7 @@ async def home() -> response:
assert response.json()["detail"] == "using JSON structure"


@pytest.mark.parametrize("response", [JSON, OrJSON, UJSON])
@pytest.mark.parametrize("response", [JSON, ORJSON, UJSON])
def test_json_datastructure_with_different_status_code(response, test_client_factory):
@get()
async def home_two() -> response:
Expand All @@ -32,7 +32,7 @@ async def home_two() -> response:
assert response.json()["detail"] == "using JSON structure"


@pytest.mark.parametrize("response", [JSON, OrJSON, UJSON])
@pytest.mark.parametrize("response", [JSON, ORJSON, UJSON])
def test_json_datastructure_with_different_status_code_on_handler(response, test_client_factory):
@get(status_code=status.HTTP_202_ACCEPTED)
async def home_three() -> response:
Expand All @@ -45,7 +45,7 @@ async def home_three() -> response:
assert response.json()["detail"] == "using JSON structure"


@pytest.mark.parametrize("response", [JSON, OrJSON, UJSON])
@pytest.mark.parametrize("response", [JSON, ORJSON, UJSON])
def test_json_datastructure_with_different_status_code_on_handler_two(
response, test_client_factory
):
Expand Down

0 comments on commit 0364c7a

Please sign in to comment.