Visualizing Async Web Apps with Bokeh and aiohttp

Feb 22, 2024 ยท 2 min read

As web applications grow more complex, visualizing and monitoring them becomes increasingly important. This is especially true for asynchronous Python web apps built with aiohttp. aiohttp allows handling multiple requests asynchronously and efficiently, but adds complexity when trying to visualize and debug your app.

That's where Bokeh comes in. Bokeh is a Python data visualization library that creates interactive visualizations in modern web browsers. By integrating Bokeh visualizations into your aiohttp web app, you can monitor and debug things like:

  • Active connections and requests
  • Request rates over time
  • Error rates and types
  • Data workflows
  • Resource usage
  • Integrating Bokeh

    Integrating Bokeh is straightforward. First install Bokeh:

    pip install bokeh

    Then in your aiohttp app, create Bokeh figure instances and use the bokeh.embed.json_item() function to generate JSON that can be included in your web pages.

    Here is an example handler that visualizes active connections:

    import aiohttp
    from bokeh.plotting import figure
    from bokeh.embed import json_item
    
    app = aiohttp.web.Application()
    
    @aiohttp_jinja2.template('index.html')
    async def index(request):
    
        p = figure(title="Active Connections")
        p.line([1, 2, 3], [4, 5, 6])
    
        plot_script = json_item(p)
    
        return {"plot_script": plot_script}

    The key is that the JSON can be directly included in your web templates and rendered into interactive plots by BokehJS.

    Tips and Tricks

  • Use Bokeh's streaming capabilities to update plots in real-time
  • Create a metrics endpoint to monitor app statistics
  • Visualize request workflows and bottlenecks
  • Integrate timing data and debug information
  • Use plots to track usage and scale resource allocation
  • With some creativity, Bokeh gives you an interactive visual debugger right in your aiohttp web app!

    Browse by tags:

    Browse by language:

    The easiest way to do Web Scraping

    Get HTML from any page with a simple API call. We handle proxy rotation, browser identities, automatic retries, CAPTCHAs, JavaScript rendering, etc automatically for you


    Try ProxiesAPI for free

    curl "http://api.proxiesapi.com/?key=API_KEY&url=https://example.com"

    <!doctype html>
    <html>
    <head>
        <title>Example Domain</title>
        <meta charset="utf-8" />
        <meta http-equiv="Content-type" content="text/html; charset=utf-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1" />
    ...

    X

    Don't leave just yet!

    Enter your email below to claim your free API key: