Skip to content

Introduction

Panther#

A Fast & Friendly Web Framework for Building Async APIs with Python 3.10+

Panther Logo

PyPI PyVersion codecov Downloads License


Why Choose Panther?#

  • One of the fastest Python frameworks available
  • Built-in file-based database (PantherDB)
  • Built-in document-oriented database ODM (Supports MongoDB & PantherDB)
  • Built-in API caching system (Supports in-memory & Redis)
  • Built-in support of OpenAPI (swagger)
  • Built-in Admin Panel
  • Native WebSocket support
  • Integrated authentication classes
  • Built-in permission handling
  • Supports custom background tasks, middlewares, and throttling
  • Offers both function-based and class-based APIs
  • Real-time API monitoring in the terminal

Benchmark#

Benchmark

[TechEmpower Benchmark]


Supported by#

JetBrains

Installation#

$ pip install panther

Getting Started#

Quick Start Guide#

  1. Create a new project directory

    $ mkdir my_panther_app
    $ cd my_panther_app
    

  2. Set up your environment

    $ python3 -m venv .venv
    $ source .venv/bin/activate  # On Windows: .\.venv\Scripts\activate
    $ pip install panther
    

  3. Create your first application

    Create a main.py file with one of the examples below.

Basic API Example#

Here's a simple REST API endpoint that returns a "Hello World" message:

main.py
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
from datetime import datetime, timedelta

from panther import status, Panther
from panther.app import GenericAPI
from panther.openapi.urls import urls as openapi_urls
from panther.response import Response


class FirstAPI(GenericAPI):
    # Response will be cached for 10 seconds for each user/ ip
    cache = timedelta(seconds=10)

    def get(self):
        current = datetime.now().isoformat()
        data = {'detail': f'Hello World | {current}'}
        return Response(data=data, status_code=status.HTTP_202_ACCEPTED)


url_routing = {
    '/': FirstAPI,
    'swagger/': openapi_urls,  # Auto generated Swagger API documentation
}
app = Panther(__name__, configs=__name__, urls=url_routing)

WebSocket Example#

Here's a simple WebSocket echo server that sends back any message it receives:

main.py
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
from panther import Panther
from panther.app import GenericAPI
from panther.response import HTMLResponse
from panther.websocket import GenericWebsocket

class EchoWebsocket(GenericWebsocket):
    async def connect(self, **kwargs):
        await self.accept()

    async def receive(self, data: str | bytes):
        await self.send(data)

class MainPage(GenericAPI):
    def get(self):
        template = """
        <input id="msg"><button onclick="s.send(msg.value)">Send</button>
        <ul id="log"></ul>
        <script>
            const s = new WebSocket('ws://127.0.0.1:8000/ws');
            s.onmessage = e => log.innerHTML += `<li><- ${msg.value}</li><li>-> ${e.data}</li>`;
        </script>
        """
        return HTMLResponse(template)

url_routing = {
    '': MainPage,
    'ws': EchoWebsocket,
}
app = Panther(__name__, configs=__name__, urls=url_routing)

Running Your Application#

  1. Start the development server

    $ panther run main:app --reload
    

    Note: Panther uses Uvicorn as the default ASGI server, but you can also use Granian, Daphne, or any ASGI-compatible server.

  2. Test your application