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 :
pip install virtualenv
This command will install the virtual environment
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.
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.
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`
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.
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