Convert Figma logo to code with AI

Kludex logostarlette

The little ASGI framework that shines. 🌟

11,516
1,045
11,516
47

Top Related Projects

11,516

The little ASGI framework that shines. 🌟

90,323

FastAPI framework, high performance, easy to learn, fast to code, ready for production

70,047

The Python micro framework for building web applications.

84,447

The Web framework for perfectionists with deadlines.

Ready-to-use and customizable users management for FastAPI

Quick Overview

Kludex/starlette is a fork of the original Starlette web framework, maintained by Marcelo Trylesinski (Kludex). This project aims to provide an alternative implementation of Starlette with additional features, bug fixes, and improvements while maintaining compatibility with the original framework.

Pros

  • Actively maintained with regular updates and bug fixes
  • Includes additional features not present in the original Starlette
  • Maintains compatibility with the original Starlette framework
  • Potentially faster development cycle for new features

Cons

  • May diverge from the original Starlette over time, leading to compatibility issues
  • Smaller community compared to the original Starlette project
  • Potential for confusion among users regarding which version to use
  • May lack some of the extensive documentation available for the original Starlette

Code Examples

  1. Basic "Hello, World!" application:
from starlette.applications import Starlette
from starlette.responses import JSONResponse
from starlette.routing import Route

async def homepage(request):
    return JSONResponse({"message": "Hello, World!"})

app = Starlette(debug=True, routes=[
    Route("/", homepage),
])
  1. Using middleware:
from starlette.applications import Starlette
from starlette.middleware import Middleware
from starlette.middleware.cors import CORSMiddleware

app = Starlette(
    middleware=[
        Middleware(CORSMiddleware, allow_origins=['*'], allow_methods=['*'])
    ]
)
  1. WebSocket example:
from starlette.applications import Starlette
from starlette.routing import WebSocketRoute

async def websocket_endpoint(websocket):
    await websocket.accept()
    await websocket.send_text("Hello, WebSocket!")
    await websocket.close()

app = Starlette(routes=[
    WebSocketRoute("/ws", websocket_endpoint)
])

Getting Started

To get started with Kludex/starlette, follow these steps:

  1. Install the package:

    pip install git+https://github.com/Kludex/starlette.git
    
  2. Create a new Python file (e.g., main.py) and add the following code:

    from starlette.applications import Starlette
    from starlette.responses import PlainTextResponse
    from starlette.routing import Route
    
    async def homepage(request):
        return PlainTextResponse("Hello, Kludex/starlette!")
    
    app = Starlette(debug=True, routes=[
        Route("/", homepage),
    ])
    
  3. Run the application using an ASGI server like Uvicorn:

    uvicorn main:app --reload
    
  4. Open your browser and navigate to http://localhost:8000 to see the application running.

Competitor Comparisons

11,516

The little ASGI framework that shines. 🌟

Pros of starlette

  • More actively maintained with regular updates
  • Larger community and ecosystem
  • Better documentation and examples

Cons of starlette

  • Potentially more complex due to additional features
  • May have a steeper learning curve for beginners
  • Slightly larger footprint

Code Comparison

starlette:

from starlette.applications import Starlette
from starlette.responses import JSONResponse
from starlette.routing import Route

async def homepage(request):
    return JSONResponse({"message": "Hello, world!"})

app = Starlette(routes=[Route("/", homepage)])

starlette>:

from starlette.applications import Starlette
from starlette.responses import JSONResponse

app = Starlette()

@app.route("/")
async def homepage(request):
    return JSONResponse({"message": "Hello, world!"})

The code comparison shows that both repositories use similar syntax and structure for creating a basic Starlette application. The main difference is in the routing approach, where starlette uses a more explicit Route definition, while starlette> uses a decorator-based approach. Both methods achieve the same result, but the choice between them may depend on personal preference or specific project requirements.

90,323

FastAPI framework, high performance, easy to learn, fast to code, ready for production

Pros of FastAPI

  • More comprehensive feature set, including automatic API documentation
  • Built-in data validation and serialization using Pydantic
  • Larger community and ecosystem, with more extensions and plugins available

Cons of FastAPI

  • Slightly higher learning curve due to additional features and concepts
  • Potentially slower performance for simple use cases due to added overhead
  • Larger codebase and dependencies compared to Starlette

Code Comparison

FastAPI example:

from fastapi import FastAPI

app = FastAPI()

@app.get("/")
async def root():
    return {"message": "Hello World"}

Starlette example:

from starlette.applications import Starlette
from starlette.routing import Route

async def homepage(request):
    return JSONResponse({"message": "Hello World"})

app = Starlette(routes=[Route("/", homepage)])

FastAPI builds upon Starlette, adding features like automatic OpenAPI documentation and request/response validation. Starlette provides a more lightweight foundation for building ASGI applications, while FastAPI offers a more opinionated and feature-rich framework. The choice between the two depends on the specific requirements of your project and your preference for simplicity versus additional built-in functionality.

70,047

The Python micro framework for building web applications.

Pros of Flask

  • Mature and well-established framework with extensive documentation
  • Large ecosystem of extensions and plugins
  • Simple and intuitive API for beginners

Cons of Flask

  • Slower performance compared to more modern ASGI frameworks
  • Lacks built-in async support (requires additional libraries)
  • More boilerplate code required for larger applications

Code Comparison

Flask:

from flask import Flask

app = Flask(__name__)

@app.route('/')
def hello():
    return 'Hello, World!'

Starlette:

from starlette.applications import Starlette
from starlette.routing import Route

async def hello(request):
    return PlainTextResponse('Hello, World!')

app = Starlette(routes=[Route('/', hello)])

The code examples show that both frameworks offer a straightforward way to create a simple "Hello, World!" application. Flask uses decorators for routing, while Starlette uses a more explicit routing configuration. Starlette's example demonstrates its native async support, which is not present in Flask's basic implementation.

While Flask has been a popular choice for Python web development for many years, Starlette offers a more modern, async-first approach with potentially better performance for certain use cases. The choice between the two depends on project requirements, developer familiarity, and performance needs.

84,447

The Web framework for perfectionists with deadlines.

Pros of Django

  • Full-featured web framework with built-in admin interface, ORM, and authentication
  • Large ecosystem with extensive documentation and third-party packages
  • Follows "batteries included" philosophy, providing tools for most common web development tasks

Cons of Django

  • Steeper learning curve due to its comprehensive nature
  • Can be overkill for smaller projects or microservices
  • Less flexibility in choosing components compared to more modular frameworks

Code Comparison

Django:

from django.http import HttpResponse

def hello_world(request):
    return HttpResponse("Hello, World!")

Starlette:

from starlette.applications import Starlette
from starlette.responses import PlainTextResponse

app = Starlette()

@app.route("/")
async def hello_world(request):
    return PlainTextResponse("Hello, World!")

Django provides a more traditional function-based view, while Starlette uses an async function with a decorator. Starlette's approach is more aligned with modern asynchronous web development practices.

Django is better suited for large, complex applications that benefit from its comprehensive features. Starlette, being a lightweight ASGI framework, is ideal for building high-performance asyncio services or smaller applications where flexibility is key.

Ready-to-use and customizable users management for FastAPI

Pros of fastapi-users

  • Specialized for user management in FastAPI applications
  • Provides ready-to-use authentication and authorization features
  • Supports multiple database backends (SQLAlchemy, MongoDB, Tortoise ORM)

Cons of fastapi-users

  • More complex setup compared to Starlette's simplicity
  • Limited to user-related functionality, less flexible for general-purpose web applications
  • Potentially steeper learning curve for developers new to FastAPI ecosystem

Code Comparison

fastapi-users:

from fastapi_users import FastAPIUsers
from fastapi_users.authentication import JWTAuthentication

fastapi_users = FastAPIUsers(
    user_db,
    [JWTAuthentication(secret="SECRET", lifetime_seconds=3600)],
)
app.include_router(fastapi_users.get_auth_router(JWTAuthentication()), prefix="/auth/jwt")

Starlette:

from starlette.applications import Starlette
from starlette.routing import Route

async def homepage(request):
    return JSONResponse({"message": "Hello, World!"})

app = Starlette(routes=[Route("/", homepage)])

The code comparison highlights the difference in focus between the two libraries. fastapi-users provides a more specialized, feature-rich solution for user management, while Starlette offers a simpler, more flexible foundation for building web applications.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

starlette-logo

✨ The little ASGI framework that shines. ✨


Build Status Package version Supported Python Version Discord


Documentation: https://starlette.dev

Source Code: https://github.com/Kludex/starlette


Starlette

Starlette is a lightweight ASGI framework/toolkit, which is ideal for building async web services in Python.

It is production-ready, and gives you the following:

  • A lightweight, low-complexity HTTP web framework.
  • WebSocket support.
  • In-process background tasks.
  • Startup and shutdown events.
  • Test client built on httpx.
  • CORS, GZip, Static Files, Streaming responses.
  • Session and Cookie support.
  • 100% test coverage.
  • 100% type annotated codebase.
  • Few hard dependencies.
  • Compatible with asyncio and trio backends.
  • Great overall performance against independent benchmarks.

Installation

$ pip install starlette

You'll also want to install an ASGI server, such as uvicorn, daphne, or hypercorn.

$ pip install uvicorn

Example

from starlette.applications import Starlette
from starlette.responses import JSONResponse
from starlette.routing import Route


async def homepage(request):
    return JSONResponse({'hello': 'world'})

routes = [
    Route("/", endpoint=homepage)
]

app = Starlette(debug=True, routes=routes)

Then run the application using Uvicorn:

$ uvicorn main:app

Dependencies

Starlette only requires anyio, and the following are optional:

  • httpx - Required if you want to use the TestClient.
  • jinja2 - Required if you want to use Jinja2Templates.
  • python-multipart - Required if you want to support form parsing, with request.form().
  • itsdangerous - Required for SessionMiddleware support.
  • pyyaml - Required for SchemaGenerator support.

You can install all of these with pip install starlette[full].

Framework or Toolkit

Starlette is designed to be used either as a complete framework, or as an ASGI toolkit. You can use any of its components independently.

from starlette.responses import PlainTextResponse


async def app(scope, receive, send):
    assert scope['type'] == 'http'
    response = PlainTextResponse('Hello, world!')
    await response(scope, receive, send)

Run the app application in example.py:

$ uvicorn example:app
INFO: Started server process [11509]
INFO: Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)

Run uvicorn with --reload to enable auto-reloading on code changes.

Modularity

The modularity that Starlette is designed on promotes building re-usable components that can be shared between any ASGI framework. This should enable an ecosystem of shared middleware and mountable applications.

The clean API separation also means it's easier to understand each component in isolation.


Starlette is BSD licensed code.
Designed & crafted with care.

— ⭐️ —