Learn Pyramid - 10 Code Examples & CST Typing Practice Test
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.
View all 10 Pyramid code examples →
Learn PYRAMID with Real Code Examples
Updated Nov 25, 2025
Code Sample Descriptions
Pyramid Simple Counter App
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()
Demonstrates a simple Pyramid app with a counter using views, routes, and templates.
Pyramid Hello World
from wsgiref.simple_server import make_server
from pyramid.config import Configurator
from pyramid.response import Response
def hello_view(request):
return Response('<h1>Hello, Pyramid!</h1>')
if __name__ == '__main__':
with Configurator() as config:
config.add_route('hello', '/')
config.add_view(hello_view, route_name='hello')
app = config.make_wsgi_app()
server = make_server('0.0.0.0', 6543, app)
print('Pyramid server running at http://localhost:6543')
server.serve_forever()
A minimal Pyramid 'Hello World' app.
Pyramid Query Parameter Example
from wsgiref.simple_server import make_server
from pyramid.config import Configurator
from pyramid.response import Response
def greet_view(request):
name = request.params.get('name', 'Guest')
return Response(f'<h1>Hello, {name}!</h1>')
if __name__ == '__main__':
with Configurator() as config:
config.add_route('greet', '/greet')
config.add_view(greet_view, route_name='greet')
app = config.make_wsgi_app()
server = make_server('0.0.0.0', 6543, app)
server.serve_forever()
Demonstrates query parameters in Pyramid routes.
Pyramid JSON Response Example
from wsgiref.simple_server import make_server
from pyramid.config import Configurator
from pyramid.response import Response
import json
def json_view(request):
data = {'message': 'Hello, Pyramid JSON'}
return Response(json.dumps(data), content_type='application/json')
if __name__ == '__main__':
with Configurator() as config:
config.add_route('json', '/json')
config.add_view(json_view, route_name='json')
app = config.make_wsgi_app()
server = make_server('0.0.0.0', 6543, app)
server.serve_forever()
Return JSON responses in Pyramid.
Pyramid POST Form Handling
from wsgiref.simple_server import make_server
from pyramid.config import Configurator
from pyramid.response import Response
from pyramid.view import view_config
def form_view(request):
name = request.params.get('name', '')
return Response(f'<h1>Hello, {name}</h1>')
if __name__ == '__main__':
with Configurator() as config:
config.add_route('form', '/form')
config.add_view(form_view, route_name='form')
app = config.make_wsgi_app()
server = make_server('0.0.0.0', 6543, app)
server.serve_forever()
Handle POST requests and form data.
Pyramid Static Files Example
from wsgiref.simple_server import make_server
from pyramid.config import Configurator
from pyramid.response import FileResponse
def static_view(request):
return FileResponse('static/index.html')
if __name__ == '__main__':
with Configurator() as config:
config.add_route('static', '/static')
config.add_view(static_view, route_name='static')
app = config.make_wsgi_app()
server = make_server('0.0.0.0', 6543, app)
server.serve_forever()
Serve static files using Pyramid.
Pyramid Template Rendering Example
from wsgiref.simple_server import make_server
from pyramid.config import Configurator
from pyramid.response import Response
from pyramid.renderers import render_to_response
def template_view(request):
return render_to_response('template.html', {'title': 'Pyramid Template'}, request=request)
if __name__ == '__main__':
with Configurator() as config:
config.add_route('template', '/template')
config.add_view(template_view, route_name='template')
app = config.make_wsgi_app()
server = make_server('0.0.0.0', 6543, app)
server.serve_forever()
Render HTML templates using Pyramid.
Pyramid Error Handling Example
from wsgiref.simple_server import make_server
from pyramid.config import Configurator
from pyramid.response import Response
from pyramid.view import view_config
def error_view(request):
raise Exception('This is an error')
if __name__ == '__main__':
with Configurator() as config:
config.add_route('error', '/error')
config.add_view(error_view, route_name='error')
app = config.make_wsgi_app()
server = make_server('0.0.0.0', 6543, app)
server.serve_forever()
Demonstrates basic error handling with Pyramid.
Pyramid Redirect Example
from wsgiref.simple_server import make_server
from pyramid.config import Configurator
from pyramid.response import Response
from pyramid.httpexceptions import HTTPFound
def redirect_view(request):
return HTTPFound(location='/counter')
if __name__ == '__main__':
with Configurator() as config:
config.add_route('redirect', '/redirect')
config.add_view(redirect_view, route_name='redirect')
app = config.make_wsgi_app()
server = make_server('0.0.0.0', 6543, app)
server.serve_forever()
Redirect requests to another URL.
Pyramid Multiple Routes Example
from wsgiref.simple_server import make_server
from pyramid.config import Configurator
from pyramid.response import Response
def home_view(request):
return Response('<h1>Home</h1>')
def about_view(request):
return Response('<h1>About</h1>')
if __name__ == '__main__':
with Configurator() as config:
config.add_route('home', '/')
config.add_view(home_view, route_name='home')
config.add_route('about', '/about')
config.add_view(about_view, route_name='about')
app = config.make_wsgi_app()
server = make_server('0.0.0.0', 6543, app)
server.serve_forever()
Handle multiple routes in one Pyramid app.
Frequently Asked Questions about Pyramid
What is Pyramid?
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.
What are the primary use cases for Pyramid?
Building small to medium web applications. Developing RESTful APIs. Rapid prototyping with customizable components. Modular enterprise apps requiring selective features. Microservices or scalable backends
What are the strengths of Pyramid?
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
What are the limitations of Pyramid?
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
How can I practice Pyramid typing speed?
CodeSpeedTest offers 10+ real Pyramid code examples for typing practice. You can measure your WPM, track accuracy, and improve your coding speed with guided exercises.