How to make random quotes rest API with Flask - Python

How to make random quotes rest API with Flask - Python

Introduction

Flask is a micro web framework written in Python. It is very easy to implement and it has no database abstraction layer, form validation.

In this article we will talk how to make a random quotes API with Flask. We will talk how to set up the virtual environment, installing python modules / libraries and more.

Prerequisites :

  • Python must be installed in your system
  • Try to use VS Code as code editor, unless it will a little bit hard to follow along
  • You must know basic of python language
  • Virtual Environment must be installed in your system , if not then

pip install virtualenv        

This command will install the virtual environment

No alt text provided for this image


Warning : If you are using Windows then, make sure to run all the commands in CMD (Command Prompt) not Power Shell. Otherwise you may face errors. Whenever I say terminal that refers to CMD not Power Shell.

Before installation make a folder and name it whatever you like and open VS code in that folder. Than open the CMD terminal.

Installation

First create a virtual environment. The command will be `virtualenv (name-you-want-to-give)`. Make sure not to use space in your name.


virtualenv env        

Here env is the name of the virtual env. You can give whatever name you like. This step will take some time.

No alt text provided for this image

Now it's time to activate the virtual environment


env\Scripts\activate        

If you give your virtual environment a different name then use that name instead of `env` in the above command.

No alt text provided for this image

If the virtual environment activates successfully, the name of your virtual environment will appear like the above picture.

Now we need to install all the necessary python libraries/module like Flask. Execute the command below to install Flask.


pip install Flask        

The installation process is finished.

Writing Code for the API

Make a python file and name it app.py. Then we will imports the libraries/modules we are going to use.


from flask import Flask,jsonify,render_template,url_for
import json
import random        

Define a quotes list


quotes = [
? ? { ?
? ? "quoteText": "Genius is one percent inspiration and ninety-nine percent perspiration.",
? ? "quoteAuthor": "Thomas Edison"
? ? }, {
? ? "quoteText": "You can observe a lot just by watching.",
? ? "quoteAuthor": "Yogi Berra"
? ? }, {
? ? "quoteText": "A house divided against itself cannot stand.",
? ? "quoteAuthor": "Abraham Lincoln"
? ? }, {
? ? "quoteText": "Difficulties increase the nearer we get to the goal.",
? ? "quoteAuthor": "Johann Wolfgang von Goethe"
? ? }, {
? ? "quoteText": "Fate is in your hands and no one elses",
? ? "quoteAuthor": "Byron Pulsifer"
? ? }, {
? ? "quoteText": "Be the chief but never the lord.",
? ? "quoteAuthor": "Lao Tzu"
? ? } ]        

Now we will create an instance of the Flask class


app = Flask(__name__)        

We will define the route for the URL `/api/quotes/random`


@app.route('/api/quotes/random')        

Let's define the function that will return the response


@app.route('/api/quotes/random')
def generate_random_quote():
   quote = random.choice(quotes)
   return jsonify(quote)         

The API is complete. Run the app.py and open the URL end point `127.0.0.1:5000/api/quotes/random`

No alt text provided for this image

This is just a basic example, you can use external APIs, database or any other method to store quotes and generate random quotes based on the requirement. Additionally, you can add more functionality like filtering, pagination etc.

Checkout my rest-quotes-api on GitHub for more reference and more functionality. Checkout the live demo, make Frontend and make Random Quote Generator.

If anyone want to know how to deploy the app free, then let me know, I will write an article in future.

---------------------------------------------------------------------------------------------------------

Thank you everyone for the support and for reading this article.

Ghamzah Samuels

Full Stack Developer/ Customer Service Advisor -I can provide simple websites, brand visibility and digital marketing.

9 个月

did you create a json file with the qoutes

回复

要查看或添加评论,请登录

Bijoy Kar的更多文章

  • Why you should start using TailwindCSS right now?

    Why you should start using TailwindCSS right now?

    Hi everyone, I am Bijoy Kar. Thank you very much for reading this article.

  • Google It Like a Pro

    Google It Like a Pro

    Hi everyone, I am Bijoy Kar. Thank you for reading Google It Like a Pro.

  • Getting Started with Python Virtual Environment

    Getting Started with Python Virtual Environment

    Hi everyone, I am Bijoy Kar. Thank you for reading Getting Started with Python Virtual Environment.

  • Git & GitHub for Beginners

    Git & GitHub for Beginners

    Hi, everyone I am Bijoy Kar. Thank you for reading Git & GitHub for Beginners.

  • Astro

    Astro

    Hi, everyone I am Bijoy Kar. Thank you for reading Astro.

  • React in Brief

    React in Brief

    Hi, everyone I am Bijoy Kar. Thank you for reading React in Brief.

  • TypeScript > JavaScript

    TypeScript > JavaScript

    Hi, everyone I am Bijoy Kar. Thank you for reading TypeScript > JavaScript Article.

  • Whys and Hows of Web Development

    Whys and Hows of Web Development

    Hi, everyone I am Bijoy Kar. Thank you for reading A to Z of Web Development Article.

  • How did I get started with coding? - A Short Story

    How did I get started with coding? - A Short Story

    Introduction This article is about the story how I get started with coding. It is not related with coding knowledge or…

  • JavaScript vs Python - Which one should you choose?

    JavaScript vs Python - Which one should you choose?

    Python and JavaScript are two of the most popular programming languages in the world. They are both widely used in web…

社区洞察

其他会员也浏览了