1. Home
  2. /
  3. Pyramid
  4. /
  5. Simple Counter App

Simple Counter App - Pyramid Typing CST Test

Loading…

Simple Counter App — Pyramid Code

Demonstrates a simple Pyramid app with a counter using views, routes, and templates.

from wsgiref.simple_server import make_server
from pyramid.config import Configurator
from pyramid.response import Response

count = 0

def counter_view(request):
	global count
	action = request.params.get('action')
	if action == 'increment': count += 1
	elif action == 'decrement': count -= 1
	elif action == 'reset': count = 0
	return Response(f'''
		<html>
		<head><title>Pyramid Counter</title></head>
		<body>
		<h2>Counter: {count}</h2>
		<form method='get'>
		<button name='action' value='increment'>+</button>
		<button name='action' value='decrement'>-</button>
		<button name='action' value='reset'>Reset</button>
		</form>
		</body>
		</html>
	''')

if __name__ == '__main__':
	with Configurator() as config:
		config.add_route('counter', '/counter')
		config.add_view(counter_view, route_name='counter')
		app = config.make_wsgi_app()

	server = make_server('0.0.0.0', 6543, app)
	print('Pyramid server running at http://localhost:6543/counter')
	server.serve_forever()

Pyramid Language Guide

Pyramid is a lightweight, flexible Python web framework that emphasizes minimalism and modularity. It allows developers to choose their own components for templating, database, and authentication.

Primary Use Cases

  • ▸Building small to medium web applications
  • ▸Developing RESTful APIs
  • ▸Rapid prototyping with customizable components
  • ▸Modular enterprise apps requiring selective features
  • ▸Microservices or scalable backends

Notable Features

  • ▸Minimalistic and modular design
  • ▸URL dispatch and traversal routing
  • ▸Pluggable templating (Jinja2, Mako, Chameleon)
  • ▸Flexible authentication and authorization policies
  • ▸Extensible via Pyramid add-ons and third-party packages

Origin & Creator

Pyramid was created by Agendaless Consulting and initially released by Chris McDonough, Michael Merickel, and the Pylons Project team in 2005.

Industrial Note

Pyramid is ideal for projects where flexibility, minimalism, and choice of components matter-common in APIs, research apps, and modular enterprise systems.

Quick Explain

  • ▸Pyramid provides a minimal core while allowing developers to add only the components they need.
  • ▸It supports URL dispatch and traversal for routing.
  • ▸Templating is pluggable, supporting Jinja2, Mako, or Chameleon.
  • ▸Includes support for authentication, authorization, and security policies.
  • ▸Used for small apps, APIs, and scalable enterprise applications requiring flexibility.

Core Features

  • ▸Configurator for app setup and configuration
  • ▸Views to handle HTTP requests
  • ▸Routing via URL dispatch or resource traversal
  • ▸Security policies for authentication/authorization
  • ▸Flexible templating and response rendering

Learning Path

  • ▸Learn Python basics
  • ▸Understand WSGI and web request/response cycle
  • ▸Learn Pyramid configurator, routing, and views
  • ▸Practice templating and pluggable components
  • ▸Build REST APIs and scalable apps with Pyramid

Practical Examples

  • ▸Simple blog website
  • ▸REST API with SQLAlchemy backend
  • ▸Research project prototype with modular components
  • ▸Enterprise dashboard with pluggable authentication
  • ▸Microservices backend with Pyramid and Celery

Comparisons

  • ▸Pyramid vs Django: Pyramid is lightweight/flexible, Django is batteries-included
  • ▸Pyramid vs Flask: Pyramid is more structured, Flask is extremely minimal
  • ▸Pyramid vs FastAPI: Pyramid is sync-first but extensible, FastAPI is async-first
  • ▸Pyramid vs Tornado: Tornado is event-driven async server, Pyramid is WSGI-based
  • ▸Pyramid vs Express.js: Python WSGI framework vs Node.js runtime framework

Strengths

  • ▸High flexibility for developers
  • ▸Lightweight core for performance
  • ▸Works for both small apps and large enterprise projects
  • ▸Supports multiple templating and database options
  • ▸Easy to integrate with modern Python libraries

Limitations

  • ▸Smaller community compared to Django or Flask
  • ▸Requires explicit configuration for many features
  • ▸No built-in ORM (SQLAlchemy or others must be integrated)
  • ▸Less beginner-friendly due to flexibility
  • ▸Documentation less beginner-focused than Django

When NOT to Use

  • ▸Developers needing full-stack out-of-the-box solutions
  • ▸Small prototypes where Flask suffices
  • ▸Projects requiring heavy default admin interfaces
  • ▸Rapid MVPs requiring built-in authentication and ORM
  • ▸Teams unfamiliar with Python WSGI apps

Cheat Sheet

  • ▸pip install pyramid -> install Pyramid
  • ▸pserve development.ini -> run server
  • ▸config.add_route -> add route
  • ▸config.add_view -> add view
  • ▸request.matchdict -> access URL parameters

FAQ

  • ▸Is Pyramid free?
  • ▸Yes - open-source under BSD license.
  • ▸Does Pyramid include ORM?
  • ▸No, you integrate your choice of ORM like SQLAlchemy.
  • ▸Is Pyramid suitable for large apps?
  • ▸Yes, highly modular and scalable.
  • ▸Can Pyramid handle REST APIs?
  • ▸Yes, fully supports REST with pluggable components.
  • ▸Is Pyramid secure?
  • ▸Yes, provides authentication/authorization frameworks and secure defaults.

30-Day Skill Plan

  • ▸Week 1: Python and Pyramid setup
  • ▸Week 2: Routing and views
  • ▸Week 3: Templating and static assets
  • ▸Week 4: Database integration with SQLAlchemy
  • ▸Week 5: Authentication, deployment, and scaling

Final Summary

  • ▸Pyramid is a lightweight, flexible Python web framework.
  • ▸Minimal core allows selective component integration.
  • ▸Supports URL routing, views, templating, and security policies.
  • ▸Suitable for small apps, APIs, and scalable enterprise systems.
  • ▸Ideal for developers who want modularity and flexibility over batteries-included frameworks.

Project Structure

  • ▸project_name/ - project root
  • ▸project_name/views/ - request handlers
  • ▸project_name/templates/ - HTML templates
  • ▸project_name/static/ - CSS, JS, images
  • ▸development.ini / production.ini - environment configs

Monetization

  • ▸APIs for SaaS products
  • ▸Backend for subscription platforms
  • ▸Internal enterprise apps
  • ▸Research project dashboards
  • ▸Web services with modular extensions

Productivity Tips

  • ▸Use Pyramid scaffolds for quick project setup
  • ▸Choose only needed components for minimal bloat
  • ▸Leverage templates and static folders effectively
  • ▸Automate testing
  • ▸Monitor logs for early issue detection

Basic Concepts

  • ▸Configurator - central object to configure Pyramid app
  • ▸Routes - map URLs to views
  • ▸Views - Python functions that handle requests
  • ▸Templates - render HTML using pluggable engines
  • ▸Security policies - authentication and authorization controls

Official Docs

  • ▸https://trypyramid.com/
  • ▸https://docs.pylonsproject.org/projects/pyramid/en/latest/

More Pyramid Typing Exercises

Pyramid Hello WorldPyramid Query Parameter ExamplePyramid JSON Response ExamplePyramid POST Form HandlingPyramid Static Files ExamplePyramid Template Rendering ExamplePyramid Error Handling ExamplePyramid Redirect ExamplePyramid Multiple Routes Example

Practice Other Languages

CReactPythonC++RustTypeScriptKotlinPHPJavaC#RubyMqlCqlN1qlCypher