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?#

Panther is designed to be fast, simple, and powerful. Here's what makes it special:

  • One of the fastest Python frameworks available
  • File-based database (PantherDB) - No external database setup required
  • Document-oriented ODM - Supports MongoDB & PantherDB with familiar syntax
  • API caching system - In-memory and Redis support
  • OpenAPI/Swagger - Auto-generated API documentation
  • WebSocket support - Real-time communication out of the box
  • Authentication & Permissions - Built-in security features
  • Background tasks - Handle long-running operations
  • Middleware & Throttling - Extensible and configurable

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
24
25
from datetime import datetime, timedelta
from panther import status, Panther
from panther.app import GenericAPI
from panther.openapi.views import ScalarOpenAPI
from panther.response import Response

class HelloAPI(GenericAPI):
    # Cache responses for 10 seconds
    cache = timedelta(seconds=10)

    def get(self):
        current_time = datetime.now().isoformat()
        return Response(
            data={'message': f'Hello from Panther! 🐾 | {current_time}'},
            status_code=status.HTTP_200_OK
        )

# URL routing configuration
url_routing = {
    '/': HelloAPI,
    'docs/': ScalarOpenAPI,  # Auto-generated API docs
}

# Create your Panther app
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
30
31
32
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()
        await self.send("Connected to Panther WebSocket!")

    async def receive(self, data: str | bytes):
        # Echo back the received message
        await self.send(f"Echo: {data}")

class WebSocketPage(GenericAPI):
    def get(self):
        template = """
        <h2>🐾 Panther WebSocket Echo Server</h2>
        <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 = {
    '': WebSocketPage,
    '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