Skip to main content

Writing FastAPI/Flask apps

Writing REST APIs inside apps can be extremely useful in Octostar. They can be used:

  • Together with the manifest (usually as transforms of type http The Manifest), to expose these functions as services in Octostar's context menus or dedicated transform menus
  • To use in conjunction with a front-end (e.g. ReactLive, or even Streamlit itself),
  • To be called by other apps
  • To be called by the automated data processing pipeline

An example of transforms in the linkchart on a node of type alias

The structure, inputs and expected outputs of the API will depend on the context in which the API is expected to be used, as in the cases described above.

BROWSER INTERACTIONS

If you are developing a back-end API, browser functionalities (e.g. from streamlit-octostar-research) will not be available. Instead, this data should be passed from the browser itself, e.g. via the manifest, or as inputs to the endpoint(s).

Endpoint Conventions​

The App Viewer and other components in Octostar assume that a (back-end) app exposes its user interface/main page under their localhost at port 8080. By convention, especially if an app has both back-end and front-end components:

  • The back-end exposes endpoint /docs for the OpenAPI documentation, endpoint /openapi.json for the JSON format of the same documentation, and //api for the REST API endpoints themselves. In particular, by convention the endpoints for the NiFi processing pipeline are expected at /api/nifi
  • The front-end exposes endpoint / for the main application page and any other endpoints if it is a multi-page app, except for those reserved above

You can clearly see the application of these rules from an example nginx.conf file.

Utilities​

Under library streamlit-octostar-utils there are some utility modules for the creation of APIs under the api_crafter package.

For FastAPI-based solutions, the module can be imported as such:

from streamlit_octostar_utils.api_crafter import fastapi

Some examples of utilities include a CommonModels for some base pydantic models useful for FastAPI requests and responses, a DefaultErrorRoute to respond in a standardized way when an exception is raised, a RequestCancelledMiddleware to raise a CancellationError if the client disconnects during a request instead of proceeding, and finally a Route class which allows developer to separate endpoint definitions from the router they should be associated with, allowing more modular code. More details are here Utilities for FastAPI.

These utilities currently do not have corresponding versions for other back-ends such as Flask or Django. Future versions of Octostar may include better support for these (and other) back-ends.