diff --git a/docs_src/src/pages/documentation/api_reference/file-uploads.mdx b/docs_src/src/pages/documentation/api_reference/file-uploads.mdx index 815b08992..171e797b1 100644 --- a/docs_src/src/pages/documentation/api_reference/file-uploads.mdx +++ b/docs_src/src/pages/documentation/api_reference/file-uploads.mdx @@ -97,6 +97,49 @@ Batman scaled his application across multiple cores for better performance. He u + +### Serving simple HTML strings + + + +Speaking of HTML files, Batman wanted to serve simple HTML strings. He was suggested to use the following code: + + + + + + ```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) + + ``` + + + + + ### Serving Other Files diff --git a/robyn/__init__.py b/robyn/__init__.py index 1bc6ae297..28cddb484 100644 --- a/robyn/__init__.py +++ b/robyn/__init__.py @@ -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 @@ -450,6 +450,7 @@ def ALLOW_CORS(app: Robyn, origins: List[str]): "jsonify", "serve_file", "serve_html", + "html", "ALLOW_CORS", "SubRouter", "AuthenticationHandler", diff --git a/robyn/responses.py b/robyn/responses.py index 7475b7903..ee70611c0 100644 --- a/robyn/responses.py +++ b/robyn/responses.py @@ -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]: """