DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
  1. DZone
  2. Coding
  3. Frameworks
  4. A Beginner-level Tutorial to Ray: A New Python Framework

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.

Damian Wolf user avatar by
Damian Wolf
·
Nov. 07, 16 · Tutorial
Like (7)
Save
Tweet
Share
22.03K Views

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

  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?

Framework Python (language)

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Project Hygiene
  • Last Chance To Take the DZone 2023 DevOps Survey and Win $250! [Closes on 1/25 at 8 AM]
  • The Role of Data Governance in Data Strategy: Part II
  • Exploring the Benefits of Cloud Computing: From IaaS, PaaS, SaaS to Google Cloud, AWS, and Microsoft

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: