Skip to content

Commit

Permalink
feat: add a way to serve pure html (#730)
Browse files Browse the repository at this point in the history
* feat: add a way to serve pure html

* add docs

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
  • Loading branch information
sansyrox and pre-commit-ci[bot] authored Jan 9, 2024
1 parent 266403b commit 8f05868
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 1 deletion.
43 changes: 43 additions & 0 deletions docs_src/src/pages/documentation/api_reference/file-uploads.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,49 @@ Batman scaled his application across multiple cores for better performance. He u
</Col>
</Row>


### Serving simple HTML strings

<Row>
<Col>
Speaking of HTML files, Batman wanted to serve simple HTML strings. He was suggested to use the following code:
</Col>
<Col sticky>

<CodeGroup title="Request" tag="GET" label="/hello_world">

```python {{ title: 'untyped' }}
from robyn import Robyn, serve_html

app = Robyn(__file__)


@app.get("/")
async def h(request):
return serve_html("./index.html")

app.start(port=8080)

```

```python {{ title: 'typed' }}
from robyn import Robyn, Request, serve_html

app = Robyn(__file__)


@app.get("/")
async def h(request: Request):
return serve_html("./index.html")

app.start(port=8080)

```
</CodeGroup>
</Col>
</Row>


### Serving Other Files


Expand Down
3 changes: 2 additions & 1 deletion robyn/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
from robyn.events import Events
from robyn.logger import logger
from robyn.processpool import run_processes
from robyn.responses import serve_file, serve_html
from robyn.responses import serve_file, serve_html, html
from robyn.robyn import FunctionInfo, HttpMethod, Request, Response, get_version, jsonify, WebSocketConnector, Headers
from robyn.router import MiddlewareRouter, MiddlewareType, Router, WebSocketRouter
from robyn.types import Directory
Expand Down Expand Up @@ -450,6 +450,7 @@ def ALLOW_CORS(app: Robyn, origins: List[str]):
"jsonify",
"serve_file",
"serve_html",
"html",
"ALLOW_CORS",
"SubRouter",
"AuthenticationHandler",
Expand Down
11 changes: 11 additions & 0 deletions robyn/responses.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
from typing import Any, Dict

from robyn.robyn import Response, Headers


def html(html: str) -> Response:
"""
This function will help in serving a simple html string
:param html str: html to serve as a response
"""
return Response(description=html, status_code=200, headers=Headers({"Content-Type": "text/html"}))


def serve_html(file_path: str) -> Dict[str, Any]:
"""
Expand Down

0 comments on commit 8f05868

Please sign in to comment.