Star us on GitHub
Star
Menu
Docs / Getting Started / For your Backend / Python Backend

Python Backend

Highlight ships the highlight_io pypi module for capturing backend errors in applications with Python backends.

Usage

The usage of this Backend SDK requires one of our Client SDKs to be installed, so please follow the instructions there if you have not done so.

If you'd like to follow an example, check out our repo for one of a Flask app and one of a backend script.

First, import the package

poetry add highlight-io # or with pip pip install highlight-io
Copy

If you are uswing a Flask app, you'll need the Flask integration.

poetry add highlight-io[Flask] # or with pip pip install highlight-io[Flask]
Copy
Adding Highlight to Flask

Import the Flask integration and setup Highlight with Flask!

from flask import Flask import highlight_io from highlight_io.integrations.flask import FlaskIntegration app = Flask(__name__) H = highlight_io.H( "YOUR_PROJECT_ID", integrations=[FlaskIntegration()], record_logs=True )
Copy
Adding Highlight to Django

Import the Django integration in your settings.py file.

import highlight_io from highlight_io.integrations.django import DjangoIntegration H = highlight_io.H("YOUR_PROJECT_ID", integrations=[DjangoIntegration()], record_logs=True)
Copy
Adding Highlight to FastAPI

Import the FastAPI middleware and setup Highlight with your FastAPI App!

from fastapi import FastAPI import highlight_io from highlight_io.integrations.fastapi import FastAPIMiddleware H = highlight_io.H("YOUR_PROJECT_ID", record_logs=True) app = FastAPI() app.add_middleware(FastAPIMiddleware) @app.get("/") async def root(): return {"message": "Hello World"}
Copy
Adding Highlight to Azure Functions

Just add the @observe_handler decorator to your functions!

import logging import azure.functions as func import highlight_io from highlight_io.integrations.azure import observe_handler H = highlight_io.H("YOUR_PROJECT_ID", record_logs=True) @observe_handler def main(req: func.HttpRequest) -> func.HttpResponse: logging.info("Python HTTP trigger function processed a request.") return func.HttpResponse("Hello, world.")
Copy
Verify

To validate your Highlight backend setup, you can setup up a testing route handler that throws an error. For example:

import logging import random import time from flask import Flask import highlight_io from highlight_io.integrations.flask import FlaskIntegration app = Flask(__name__) H = highlight_io.H( "YOUR_PROJECT_ID", integrations=[FlaskIntegration()], record_logs=True ) @app.route("/error") def hello(): for idx in range(1000): logging.info(f"hello {idx}") time.sleep(0.001) if random.randint(0, 100) == 1: raise Exception(f"random error! {idx}") logging.warning("made it outside the loop!") return "<h1>Hello, World!</h1>" if __name__ == "__main__": app.run()
Copy

creates a /error route that will randomly throw an error.

To view and resolve the recorded error, log into app.highlight.io/errors. Clicking on the error's title will open a page where you can see detailed information and mark it as resolved.