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.

Overview

Classes

Classes

class apifrom.decorators.web.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)[source]
Parameters:

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.

apifrom.decorators.web.router

The router instance for managing routes.

Type:

Router

apifrom.decorators.web.middleware

List of middleware instances.

Type:

list

apifrom.decorators.web.debug

Whether to run in debug mode.

Type:

bool

apifrom.decorators.web.title

The title of the API.

Type:

str

apifrom.decorators.web.description

The description of the API.

Type:

str

apifrom.decorators.web.version

The version of the API.

Type:

str

apifrom.decorators.web.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.

class apifrom.decorators.web.Response(content=None, status_code=200, headers=None, content_type='application/json')[source]
Parameters:
  • content (Any)

  • status_code (int)

  • headers (Optional[Dict[str, str]])

  • content_type (str)

Response class for APIFromAnything.

This class represents an HTTP response and provides methods for setting response data, status code, and headers.

apifrom.decorators.web.content

The response content.

apifrom.decorators.web.status_code

The HTTP status code.

apifrom.decorators.web.headers

HTTP headers.

apifrom.decorators.web.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.

class apifrom.decorators.web.Web(title=None, description=None, theme='default', template=None)[source]
Parameters:
  • title (Optional[str])

  • description (Optional[str])

  • theme (str)

  • template (Optional[str])

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.

apifrom.decorators.web.title

The title to display in the HTML page

Type:

str

apifrom.decorators.web.description

A description of the endpoint to display in the HTML

Type:

str

apifrom.decorators.web.theme

The theme to use for styling (‘default’, ‘dark’, ‘light’)

Type:

str

apifrom.decorators.web.template

Optional path to a custom HTML template

Type:

str

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

apifrom.decorators.web.__call__(func)

Apply the decorator to the function.

param func:

The function to decorate

returns:

The decorated function

apifrom.decorators.web._format_value(value)

Format a value for HTML display.

param value:

The value to format

returns:

The formatted value

apifrom.decorators.web._get_theme_styles()

Get the CSS styles for the current theme.

returns:

The CSS styles for the theme

apifrom.decorators.web._render_dict(data)

Render a dictionary as HTML.

param data:

The dictionary to render

returns:

The HTML representation of the dictionary

apifrom.decorators.web._render_html(data)

Render the data as HTML.

param data:

The data to render

returns:

The HTML representation of the data

apifrom.decorators.web._render_list(data)

Render a list as HTML.

param data:

The list to render

returns:

The HTML representation of the list

apifrom.decorators.web._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