Skip to content

Single page Django app via Carlton Gibson's DjangoCon talk!

Notifications You must be signed in to change notification settings

wsvincent/django-microframework

Repository files navigation

µDjango (Django as a Microframework)

How close can Django get to Flask's five-line "Hello, World!" implementation?

Carlton Gibson gave a talk at DjangoCon US 2019, Using Django as a Micro-Framework, where he demonstrated a single file implementation of "Hello, World!" in Django.

This repo demonstrates his original code example and subsequent attempts to display "Hello, World!" in a single file in as few lines of code as possible.

Set Up

On the command line navigate to a directory, create and activate a new Python virtual environment, and install Django via pip.

Windows (PowerShell)

# Windows
$ python -m venv .venv
$ .venv\Scripts\Activate.ps1
(.venv) $ python -m pip install django~=5.0.0

macOS or GNU/Linux

$ python3 -m venv .venv
$ source .venv/bin/activate
(.venv) $ python -m pip install django~=5.0.0

Option 1: Carlton Gibson

# hello_django1.py
from django.conf import settings
from django.core.handlers.wsgi import WSGIHandler
from django.http import HttpResponse
from django.urls import path

settings.configure(
    ROOT_URLCONF=__name__,
)

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

urlpatterns = [
    path("", hello_world)
]

application = WSGIHandler()

Install Gunicorn to run the local server.

(.venv) $ python -m pip install gunicorn==22.0.0

Start the server.

(.venv) $ gunicorn hello_django:application

Navigate to http://127.0.0.1:8000. To stop the Gunicorn server, use Ctrl+c on the command line.

Peter offered an update using execute_from_command_line to make python hello_django.py the equivalent of running Django's manage.py command. It also does not need Gunicorn to be installed.

# hello_django2.py
from django.conf import settings
from django.core.handlers.wsgi import WSGIHandler
from django.core.management import execute_from_command_line  # new
from django.http import HttpResponse
from django.urls import path

settings.configure(
    ROOT_URLCONF=__name__,
    DEBUG=True,  # new
)

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

urlpatterns = [
    path("", hello_world)
]

application = WSGIHandler()

if __name__ == "__main__":  # new
    execute_from_command_line()

Then start the server with Django's runserver command.

(.venv) $ python hello_django1.py runserver

And navigate to http://127.0.0.1:8000.

Paolo further decreased the size of the file using lambda instead of the function, reduced the memory usage using ALLOWED_HOSTS instead of DEBUG, and made it possible to use the code with runserver or gunicorn.

# hello_django2.py
from django.conf import settings
from django.core.handlers.wsgi import WSGIHandler
from django.core.management import execute_from_command_line
from django.http import HttpResponse
from django.urls import path

settings.configure(
    ALLOWED_HOSTS="*",  # new
    ROOT_URLCONF=__name__,
)

urlpatterns = [path("", lambda request: HttpResponse("Hello, Django!"))]  # new

if __name__ == "__main__":
    execute_from_command_line()
else:  # new
    application = WSGIHandler()

Run

runserver

Start the server with Django's runserver command.

(.venv) $ python hello_django2.py runserver

gunicorn

Install Gunicorn to run the local server.

(.venv) $ python -m pip install gunicorn==21.2.0

Start the server with the gunicorn command.

(.venv) $ gunicorn hello_django2:application

Test

Navigate to http://127.0.0.1:8000.

To stop the runserver or gunicorn, use Ctrl+c on the command line.

Option 3b: Paolo Melchiorre

At the DjangoCon US 2023 sprints, Paolo presented a new version of this file that uses ASGI and uvicorn to return the JSON response "Hello World".

Install uvicorn along with the existing Django installation.

(.venv) $ python -m pip install uvicorn

Create a new file called hello_django3.py and update it as follows:

# hello_django3.py
from django import conf, http, urls
from django.core.handlers.asgi import ASGIHandler

conf.settings.configure(ALLOWED_HOSTS="*", ROOT_URLCONF=__name__)

app = ASGIHandler()


async def root(request):
    return http.JsonResponse({"message": "Hello World"})


urlpatterns = [urls.path("", root)]

Start the server with the uvicorn command:

(.venv) $ uvicorn hello_django3:app --reload

Open your browser at http://127.0.0.1:8000 and the JSON response is:

{ "message": "Hello World" }

Option 4: Andrew Godwin

In March 2024, Andrew Godwin released a small library that makes it easier to write single-file Django applications in a similar way to how you'd write Flask applications. First, install the library.

(.venv) $ python -m pip install django-singlefile

Then create a file called hello_django4.py with the following code:

# hello_django4.py
from django.http import HttpResponse
from django.singlefile import SingleFileApp

app = SingleFileApp()


@app.path("")
def index(request):
    name = request.GET.get("name", "World")
    return HttpResponse(f"Hello, {name}!")


if __name__ == "__main__":
    app.main()

To run the app you can call it from the command line:

(.venv) $ python hello_django4.py runserver

About

Single page Django app via Carlton Gibson's DjangoCon talk!

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages