How to create REST API using Pyramid

Apcelent
1 min readMar 3, 2018

--

Pyramid is a lightweight python web framework following MVC architectural pattern. In this article, we will set up a web application and API using Pyramid.

Pyramid is a suitable framework for large scale MVC applications and it comes with flexible bootstrapping tools.

Cornice, a library from Mozilla, makes it easy to develop RESTful web services with pyramid.

Installation

First we create a virtual environment which helps us in separating project specific python packages installations from other projects. After activating the virtual environment, we install pyramid framework using command:

pip install pyramid

Create models

We create a models.py file with a class Note which maps to the data table storing all the values of notes.

# pyramidapp/models.pyclass Note(Base):
__tablename__ = 'Note'
id = Column(Integer, primary_key=True)
title = Column(Text)
description = Column(Text)
create_at = Column(Text)
create_by = Column(Text)
priority = Column(Integer)
def __init__(self, title, description, create_at ,create_by, priority):
self.title = title
self.description = description
self.create_at = create_at
self.create_by = create_by
self.priority = priority
@classmethod
def from_json(cls, data):
return cls(**data)
def to_json(self):
to_serialize = ['id', 'title', 'description', 'create_at', 'create_by', 'priority']
d = {}
for attr_name in to_serialize:
d[attr_name] = getattr(self, attr_name)
return d

To read more click on http://blog.apcelent.com/create-rest-api-using-pyramid.html

--

--