Over a million developers have joined DZone.

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.

· Web Dev Zone

Start coding today to experience the powerful engine that drives data application’s development, brought to you in partnership with Qlik.

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

  1. API development with Ray Framework is easy and fun

  2. Comes with the uWSGI built-in server for hosting purposes

  3. It is compatible with Pewee, Google App Engine, and SQLAlchemy.

  4. Offers API Protection

  5. Database Hooks and Actions in APIs are also supported

  6. 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:

  1. Peewee ORM

  2. Ray Framework

  3. 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

1-Ray-framework-installation.JPG

Note: Make sure you run commands using sudo or a root account.

3-successful-installation.JPG

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.

4-no-resources-found.JPG

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?

Create data driven applications in Qlik’s free and easy to use coding environment, brought to you in partnership with Qlik.

Topics:
frameworks ,python ,web dev

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}