apifrom.decorators.web ====================== Web decorator for the APIFromAnything library. This module provides the Web decorator, which enhances API endpoints with automatic HTML rendering capabilities, making them more user-friendly when accessed directly from a web browser. .. py:currentmodule:: apifrom.decorators.web Overview -------- **Classes** * :py:class:`API` * :py:class:`Response` * :py:class:`Web` Classes ------- .. py:class:: API(debug = False, title = 'APIFromAnything API', description = 'API created with APIFromAnything', version = '1.0.0', docs_url = '/docs', openapi_config = None, swagger_ui_config = None, enable_docs = True) Main application container for APIFromAnything. This class serves as the central registry for all API endpoints, middleware, and configuration. It provides methods for registering endpoints, middleware, and starting the server. .. attribute:: router The router instance for managing routes. :type: Router .. attribute:: middleware List of middleware instances. :type: list .. attribute:: debug Whether to run in debug mode. :type: bool .. attribute:: title The title of the API. :type: str .. attribute:: description The description of the API. :type: str .. attribute:: version The version of the API. :type: str .. attribute:: docs_url The URL for the API documentation. :type: str Initialize a new API instance. :param debug: Whether to run in debug mode. :param title: The title of the API. :param description: The description of the API. :param version: The version of the API. :param docs_url: The URL for the API documentation. :param openapi_config: Configuration for OpenAPI documentation. :param swagger_ui_config: Configuration for Swagger UI. :param enable_docs: Whether to enable API documentation. .. py:class:: Response(content = None, status_code = 200, headers = None, content_type = 'application/json') Response class for APIFromAnything. This class represents an HTTP response and provides methods for setting response data, status code, and headers. .. attribute:: content The response content. .. attribute:: status_code The HTTP status code. .. attribute:: headers HTTP headers. .. attribute:: content_type The content type of the response. Initialize a new Response instance. :param content: The response content. :param status_code: The HTTP status code. :param headers: HTTP headers. :param content_type: The content type of the response. .. py:class:: Web(title = None, description = None, theme = 'default', template = None) Decorator that enhances API endpoints with HTML rendering capabilities. When an endpoint decorated with @Web is accessed with an Accept header that includes 'text/html' (e.g., from a web browser), the response will be rendered as HTML. Otherwise, the response will be returned as JSON (the default API behavior). This makes API endpoints more user-friendly when accessed directly from a web browser, while maintaining their programmatic API functionality. .. attribute:: title The title to display in the HTML page :type: str .. attribute:: description A description of the endpoint to display in the HTML :type: str .. attribute:: theme The theme to use for styling ('default', 'dark', 'light') :type: str .. attribute:: template Optional path to a custom HTML template :type: str .. admonition:: Example ```python from apifrom.core.app import API from apifrom.decorators.web import Web app = API() @app.api('/users') @Web(title="Users API", description="Get a list of users") def get_users(request): return [ {"id": 1, "name": "Alice"}, {"id": 2, "name": "Bob"}, {"id": 3, "name": "Charlie"} ] ``` Initialize the Web decorator. :param title: The title to display in the HTML page :param description: A description of the endpoint to display in the HTML :param theme: The theme to use for styling ('default', 'dark', 'light') :param template: Optional path to a custom HTML template .. :: description .. :: template .. :: theme .. :: title .. method:: __call__(func) Apply the decorator to the function. :param func: The function to decorate :returns: The decorated function .. method:: _format_value(value) Format a value for HTML display. :param value: The value to format :returns: The formatted value .. method:: _get_theme_styles() Get the CSS styles for the current theme. :returns: The CSS styles for the theme .. method:: _render_dict(data) Render a dictionary as HTML. :param data: The dictionary to render :returns: The HTML representation of the dictionary .. method:: _render_html(data) Render the data as HTML. :param data: The data to render :returns: The HTML representation of the data .. method:: _render_list(data) Render a list as HTML. :param data: The list to render :returns: The HTML representation of the list .. method:: _render_with_template(data) Render the data using a custom template. :param data: The data to render :returns: The HTML representation of the data using the template