What is API? Python Tutorial
Day 17 in #100DaysOfCode
Python is a programming language that allows you to build a variety of online apps, websites, APIs, and desktop programs using frameworks. A connection between computers or computer programs is known as an application programming interface (API). It's a form of software interface that provides a service to other programs. API is a tool that allows you to get data from a database and use it in your projects in whatever way you want.
Flask is a web framework and a Python module that makes it simple to create web applications. This lesson requires Python 3, the Flask web framework, and a web browser, and installation instructions for all platforms are provided below. Python 3 and the Flask web framework are required for this tutorial.
What is the purpose of a virtual environment?
To build an isolated Python environment for distinct tasks, a virtual environment is utilized. Because various projects have distinct dependencies, we construct virtual environments. It also aids in the maintenance of the global packages folder.
We should create a file. I will call it a "tutorial". We should go to the terminal and type these:
mkdir tutorial
cd tutorial
touch main.py
python3 -m venv env
source env/bin/activate
pip install Flask
pip3 install flask-restful
I want to write the Bene Gesserit Litany from "Dune" by Frank Herbert for starters as an example Then we are going to the main.py and type this:
from flask import Flask
from flask_restful import Resource, Api
app = Flask(__name__)
api = Api(app)
class Book(Resource):
def get(self):
return {
'Frank Herbert': {
'The Bene Gesserit Litany Against Fear.': ['I must not fear. Fear is the mind-killer. Fear is the little death that brings total obliteration.',
]
},
}
api.add_resource(Book, '/')
if __name__ == '__main__':
app.run(debug=True)
When we run the program, we receive a code like this:
* Serving Flask app 'app' (lazy loading)
* Environment: production
WARNING: This is a development server. Do not use it in a production deployment.
Use a production WSGI server instead.
* Debug mode: on
* Running on http://127.0.0.1:5000 (Press CTRL+C to quit)
* Restarting with stat
* Debugger is active!
* Debugger PIN: 119-963-348
127.0.0.1 - - [20/Apr/2022 16:01:36] "GET / HTTP/1.1" 200 -
When clicked on the " http://127.0.0.1:5000 ", we can go to our page on our browser.