quickstart

Start by installing the package with your favorite package manager:

uv add spiderweb-framework
pip install spiderweb-framework
pipenv install spiderweb-framework

Then, create a new file and drop this in it:

from spiderweb import SpiderwebRouter
from spiderweb.response import HttpResponse

app = SpiderwebRouter()

@app.route("/")
def index(request):
    return HttpResponse("HELLO, WORLD!")

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

Start the dev server by running python {yourfile.py} and navigating to http://localhost:8000/ in your browser. You should see HELLO, WORLD! displayed on the page. Press Ctrl+C to stop the server.

That's it! You've got a working web app. Let's take a look at what these few lines of code are doing:

from spiderweb import SpiderwebRouter

The SpiderwebRouter class is the main object that everything stems from in Spiderweb. It's where you'll set your options, your routes, and more.

from spiderweb.response import HttpResponse

Rather than trying to infer what you want, Spiderweb wants you to be specific about what you want it to do. Part of that is the One Response Rule:

Every view must return a Response, and each Response must be a specific type.

There are five different types of responses; if you want to skip ahead, hop over to the responses page to learn more. For this example, we'll focus on HttpResponse, which is the base response.

app = SpiderwebRouter()

This line creates a new instance of the SpiderwebRouter class and assigns it to the variable app. This is the object that will handle all of your requests and responses. If you need to pass any options into Spiderweb, you'll do that here.

@app.route("/")
def index(request):
    return HttpResponse("HELLO, WORLD!")

This is an example view. There are a few things to note here:

  • The @app.route("/") decorator tells spiderweb that this view should be called when the user navigates to the root of the site. There are three different ways to declare this, but this is the easiest for demo purposes.
  • The def index(request): function is the view itself. It takes a single argument, request, which is a Request object that contains all the information about the incoming request.
  • The return HttpResponse("HELLO, WORLD!") line is the response. In this case, it's a simple HttpResponse object that contains the string HELLO, WORLD!. This will be sent back to the user's browser.

See declaring routes for more information.

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

Once you finish setting up your app, it's time to start it! You can start the dev server by just calling app.start() (and its counterpart app.stop(), or just CTRL+C). This will start a simple server on localhost:8000 that you can access in your browser.

Now that your app is done, you can also run it with Gunicorn by running gunicorn --workers=2 {yourfile}:app in your terminal. This will start a Gunicorn server on localhost:8000 that you can access in your browser and is a bit more robust than the dev server.

application paths

SpiderwebRouter(base_dir=...) sets the root for application files. It accepts a string or pathlib.Path. If omitted, the root is the working directory when the router is constructed, not the location of the application file or Spiderweb's installation. A relative base_dir is resolved against that working directory. The absolute root is available as app.BASE_DIR.

If you want to specify a working directory for the app to use, set it explicitly on the router object:

from pathlib import Path

from spiderweb import SpiderwebRouter

app = SpiderwebRouter(
    base_dir=Path(__file__).resolve().parent,
    templates_dirs=["templates"],
    staticfiles_dirs=["static_files"],
    media_dir="media",
    db="spiderweb.db",
)

Relative templates_dirs, staticfiles_dirs, media_dir, and SQLite filename strings passed as db are anchored to this root during initialization. Absolute paths are preserved. Changing the working directory later does not redirect these files. Template directories retain their configured search order, with framework templates last as a fallback. SQLAlchemy engines and database URL strings are passed through unchanged, so a URL such as sqlite:///local.db still follows SQLAlchemy's working-directory-relative behavior.

upgrading existing applications

Prior to 2.8.2, earlier versions kinda worked by accident because I messed up the path routing. Paths now consistently use the application root.

If your application was written for a version before 2.8.2, you might need to adjust some parts of it for the fixed functionality. Nothing will be moved automatically. Before upgrading, locate the current database and uploads, back them up, and set base_dir to the previous root or use absolute paths to keep using them. If templates live elsewhere, configure their absolute path separately. The same explicit absolute paths can be used with earlier versions when rolling back. To move data to a new root, stop your app and perform that transfer separately; pointing at a different root otherwise creates a new database rather than opening the old one.

debug mode

Pass debug=True when creating your app to get more information when something goes wrong:

app = SpiderwebRouter(debug=True)

With debug mode on, any exception raised while handling a request returns a plain HTML page showing the exception type, message, and full traceback, instead of a bare error body. This includes Spiderweb's own HTTP exceptions (like ServerError or NotFound raised from a view) -- the page keeps the exception's status code (a raised NotFound shows as 404, not 500). Requests that match no route at all still use your error routes, so a plain 404 page is unaffected. This works in both WSGI and ASGI mode.