A Beginner-level Tutorial to Ray: A New Python Framework
In this article, we will focus on exploring the different features of Ray framework and also build a simple blog using the framework.
Join the DZone community and get the full member experience.
Join For Free
Almost every single day technology advances to new depths. New frameworks, libraries, design patterns, and programming ideas are released to keep up with the innovation rate in other sectors. Social-coding platforms such as Livecoding, GitHub, StackOverFlow, etc., have also seen an increase in interactivity as programmers tend to try new technology.
One such technology, Ray a Python Framework, is also making news. It is developed by Felipe Volpone, and the initial impression looks promising. It should also be noted that it is made with inspiration from Django and Flask, combining them so you can create powerful REST APIs. In this article, we will focus on exploring the different features of Ray framework and also build a simple blog using the framework.
Before we get started, let’s go through the key features of the Ray framework.
Key Features of Ray Framework
API development with Ray Framework is easy and fun
Comes with the uWSGI built-in server for hosting purposes
It is compatible with Pewee, Google App Engine, and SQLAlchemy.
Offers API Protection
Database Hooks and Actions in APIs are also supported
Supports authentication.
A Beginner-Level Tutorial to Ray - A New Python Framework
Installing Ray Framework
To use Ray Framework, you need to install the basic, prerequisite software and libraries to make it functional. For proper functioning and installation, you need to install the following:
Peewee ORM
Ray Framework
Ray Peewee.
To do so, you need to run the following pip commands in your linux terminal. (Note: Ubuntu 16.04 is used for the tutorial).
pip install peewee
pip install ray_framework
pip install ray_peewee
Note: Make sure you run commands using sudo or a root account.
Creating App.py for the Model Post
Posts are an important part of any blog as they constitute the core idea behind a blog. Creating a model will ensure they are shown correctly, and interact with the database. To do so, we need to create “app.py” file with the model post. Check the code below on how to approach it:
app.py
# app.py
import peewee
from ray_peewee.all import PeeweeModel
from ray.wsgi.wsgi import application
from ray.endpoint import endpoint
database = peewee.SqliteDatabase('example.db')
class DBModel(PeeweeModel):
class Meta:
database = database
@endpoint('/post')
class Post(DBModel):
title = peewee.CharField()
description = peewee.TextField()
database.create_tables([Post])
Once done, you need to run the application using the following command:
ray up --wsgifile=app.py
# if you're using virtualenv
ray up --wsgifile=app.py --env <env_dir>
We are not using a virtualevn, so you can use the first command to run the server. Now you can access the server by typing localhost:8080 in the address bar.
As we don’t have any resource or data set up, it will give a 404 Not Found error.
Now we will go ahead and create a post using the CURL method.
curl -X POST -H "Content-Type: application/json" -d '{
"title": "New blog!",
"description": "let’s do this"
}'"http://localhost:8080/api/post"
You can use the following CURL methods to interact with your blog:
1. Listing all blog posts
curl -X GET "http://localhost:8080/api/post/"
2. Retrieving one post
curl -X GET "http://localhost:8080/api/post/1"
3. Record searching
curl -X GET "http://localhost:8080/api/post?name=john"
4. Updating post using CURL
curl -X PUT -H "Content-Type: application/json" -d '{"title": "let’s change the title."}'"http://localhost:8080/api/post/1"
5. Deleting a post
curl -X DELETE "http://localhost:8080/api/post/1"
And there you go, you have successfully setup your blog using Ray framework. You can read more about Ray features such as Endpoints, hooks, authentications, etc., in the Medium article here. It is written by the Ray framework author, Felipe Volpone, himself!
Did you find the beginner-level tutorial to Ray framework useful? If so, comment and let us know. Also, share your thoughts on how you think Ray framework can impact Python web development. Is it worth the attention?
Opinions expressed by DZone contributors are their own.
Comments