Learn PYRAMID with Real Code Examples
Updated Nov 25, 2025
Code Sample Descriptions
1
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.
2
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.
3
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.
4
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.
5
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.
6
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.
7
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.
8
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.
9
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.
10
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.