flask_jsonrpc.contrib.browse package

Module contents

register_middleware(name)[source]

Register a browse middleware.

If a middleware returns:
  • True: the middleware will be kept for after_request and teardown_request processing.

  • False: the middleware will be skipped for after_request and teardown_request processing.

  • A Response: the response will be returned immediately, skipping further processing.

Parameters:

name (str) – The name of the middleware.

Returns:

The middleware decorator.

Return type:

Callable

Examples

Inject a custom header into the response using a middleware:

>>> import pytest
>>> pytest.skip('The global context causes issues with testing.')
>>>
>>> from flask import Flask, Response
>>> from flask_jsonrpc.contrib.browse import JSONRPCBrowse, register_middleware
>>>
>>> app = Flask(__name__)
>>> browse = JSONRPCBrowse(app)
>>>
>>> @register_middleware('example_middleware')
... def example_middleware(request):
...     response = yield
...     response.headers['X-Example'] = 'Value'
...     yield response
>>>
>>> with app.test_client() as client:
...     response = client.get('/api/browse/')
...     assert response.headers.get('X-Example') == 'Value'
build_package_tree(service_methods)[source]

Build a package tree from service methods.

Parameters:

service_methods (dict[str, flask_jsonrpc.typing.Method]) – The service methods.

Returns:

The package tree as a dictionary.

Return type:

dict[str, Any]

class JSONRPCBrowseTemplateLoader(app_jinja_loader, browse_jinja_loader)[source]

Bases: BaseLoader

Custom Jinja2 template loader that tries to load templates from the application first, then falls back to the browse templates.

Parameters:
  • app_jinja_loader (jinja2.BaseLoader) – The application’s Jinja2 template loader.

  • browse_jinja_loader (jinja2.BaseLoader) – The browse’s Jinja2 template loader.

app_jinja_loader

The application’s Jinja2 template loader.

Type:

jinja2.BaseLoader

browse_jinja_loader

The browse’s Jinja2 template loader.

Type:

jinja2.BaseLoader

get_source(environment, template)[source]

Get the source of a template.

If the template is not found in the application loader, it tries to load it from the browse loader.

Parameters:
  • environment (jinja2.Environment) – The Jinja2 environment.

  • template (str) – The name of the template.

Returns:

The template source

Return type:

tuple[str, str | None, Callable[[], bool] | None]

class JSONRPCBrowse(app=None, url_prefix='/api/browse', base_url=None)[source]

Bases: object

JSON-RPC Browse extension for Flask applications.

Parameters:
  • app (Flask | None) – The Flask application to initialize the extension with.

  • url_prefix (str) – The URL prefix for the browse interface.

  • base_url (str | None) – The base URL for the browse interface.

url_prefix

The URL prefix for the browse interface.

Type:

str

base_url

The base URL for the browse interface.

Type:

str | None

jsonrpc_sites

The set of registered JSON-RPC sites

Type:

set[flask_jsonrpc.site.JSONRPCSite]

Examples

>>> from flask import Flask
>>> from flask_jsonrpc import JSONRPCBlueprint
>>> from flask_jsonrpc.contrib.browse import JSONRPCBrowse
>>>
>>> app = Flask(__name__)
>>> jsonrpc = JSONRPCBlueprint('example', __name__)
>>> browse = JSONRPCBrowse(app, url_prefix='/api/browse')
>>> browse.register_jsonrpc_site(jsonrpc)
init_app(app)[source]

Initialize the JSON-RPC Browse extension with a Flask application.

Middlewares for before_request, after_request, and teardown_request are registered.

Parameters:
Return type:

None

Examples

>>> from flask import Flask
>>> from flask_jsonrpc.contrib.browse import JSONRPCBrowse
>>>
>>> app = Flask(__name__)
>>> browse = JSONRPCBrowse()
>>> browse.init_app(app)
register_jsonrpc_site(jsonrpc_site)[source]

Register a JSON-RPC site with the browse extension.

Parameters:
Return type:

None

get_browse_title()[source]

Get the browse title.

Register a custom title by setting BROWSE_TITLE in your settings or overriding this method.

Returns:

The browse title.

Return type:

str

Parameters:

self (Self)

get_browse_title_url()[source]

Get the browse title URL.

Register a custom title URL by setting BROWSE_TITLE_URL in your settings or overriding this method.

Returns:

The browse title URL.

Return type:

str

Parameters:

self (Self)

get_browse_subtitle()[source]

Get the browse subtitle.

Register a custom subtitle by setting BROWSE_SUBTITLE in your settings or overriding this method.

Returns:

The browse subtitle.

Return type:

str

Parameters:

self (Self)

get_browse_description()[source]

Get the browse description.

Register a custom description by setting BROWSE_DESCRIPTION in your settings or overriding this method.

Returns:

The browse description.

Return type:

str

Parameters:

self (Self)

get_browse_fork_me_button_enabled()[source]

Check if the “Fork me on GitHub” button is enabled.

Register the button state by setting BROWSE_FORK_ME_BUTTON_ENABLED in your settings or overriding this method.

Returns:

True if the “Fork me on GitHub” button is enabled, False otherwise.

Return type:

bool

Parameters:

self (Self)

get_browse_media_css()[source]

Get the browse media CSS files.

Register custom CSS files by setting BROWSE_MEDIA_CSS in your settings or overriding this method.

Returns:

The browse media CSS files.

Return type:

dict[str, list[str]]

Parameters:

self (Self)

get_browse_media_js()[source]

Get the browse media JS files.

Register custom JS files by setting BROWSE_MEDIA_JS in your settings or overriding this method.

Returns:

The browse media JS files.

Return type:

list[str]

Parameters:

self (Self)

get_browse_dashboard_menu_name()[source]

Get the browse dashboard menu name.

Register a custom dashboard menu name by setting BROWSE_DASHBOARD_MENU_NAME in your settings or overriding this method.

Returns:

The browse dashboard menu name.

Return type:

str

Parameters:

self (Self)

get_browse_dashboard_partial_template()[source]

Get the browse dashboard partial template.

Register a custom dashboard partial template by setting BROWSE_DASHBOARD_PARTIAL_TEMPLATE in your settings or overriding this method.

Returns:

The browse dashboard partial template.

Return type:

str

Parameters:

self (Self)

get_browse_login_template()[source]

Get the browse login template.

Register a custom login template by setting BROWSE_LOGIN_TEMPLATE in your settings or overriding this method.

Returns:

The browse login template or None if not configured.

Return type:

str | None

Parameters:

self (Self)

get_browse_logout_template()[source]

Get the browse logout template.

Register a custom logout template by setting BROWSE_LOGOUT_TEMPLATE in your settings or overriding this method.

Returns:

The browse logout template or None if not configured.

Return type:

str | None

Parameters:

self (Self)

vf_index()[source]

Render the index page.

Returns:

The rendered index page.

Return type:

str

Parameters:

self (Self)

vf_login()[source]

Render the login page.

Returns:

The rendered login page or an error message with status code if not configured.

Return type:

str | tuple[str, int]

Parameters:

self (Self)

vf_logout()[source]

Render the logout page.

Returns:

The rendered logout page or an error message with status code if not configured.

Return type:

str | tuple[str, int]

Parameters:

self (Self)

vf_json_packages()[source]

Get the JSON representation of the package tree.

Ignores methods starting with ‘rpc.’ as they are reserved for JSON-RPC 2.0 specification system extensions.

Returns:

The JSON representation of the package tree.

Return type:

flask.typing.ResponseReturnValue

Parameters:

self (Self)

vf_json_method(method_name)[source]

Get the JSON representation of a specific method.

Parameters:
  • method_name (str) – The name of the method.

  • self (Self)

Returns:

The JSON representation of the method or a 404 error if not found.

Return type:

flask.typing.ResponseReturnValue

vf_partials_menu_tree()[source]

Render the menu tree partial template.

Returns:

The rendered menu tree partial template.

Return type:

str

Parameters:

self (Self)

vf_partials_dashboard()[source]

Render the dashboard partial template.

Returns:

The rendered dashboard partial template.

Return type:

str

Parameters:

self (Self)

vf_partials_field_describe()[source]

Render the field describe partial template.

Returns:

The rendered field describe partial template.

Return type:

str

Parameters:

self (Self)

vf_partials_response_object()[source]

Render the response object partial template.

Returns:

The rendered response object partial template.

Return type:

str

Parameters:

self (Self)