Help
Community
Project privacy set to public. By default, its content is available to everyone (authenticated or not). Please note that more restrictive permissions might exist on some items.
API without authentication
from flask import Flask jsonify app = flask( _name_ ) @app.route('/hello', methods = ['GET']) def hello _ world(): return jsonify({"message":"Hello.world!"}) if_name_=="_main_"; app.run(debug=True)
1 Importing Flask and JSON Response Function
from flask import Flask jsonify flask: is framework, help to build the web application and API jsonify: This is used to convert python data(like dictionaries) into JSON format which is the format APIs us to send data
2 Creating the Flask app
app = flask( _name_ ) flask( _name_ ): it creates a flask application object. the _name_ variable tells to flask that this is main file where app is defined
3 Defining API endpoint
@app.route('/hello', methods = ['GET']) def hello _ world(): return jsonify({"message":"Hello.world!"}) 1. set up URL endpoint /hello. methods = ['GET'] this endpoint only accept GET request(used for retrieving data) 2. hello _ world(): this function will run when someone accesses the /hello endpoint. 3. return jsonify({"message":"Hello.world!"}) It returns a JSON response with the message
4 Running Flask App
if_name_=="_main_"; app.run(debug=True) 1. this checks if the script is being run directly (not imported from another file) 2. second line the flask development server debug=True, this one allows automatic reloading when we make code changes and shows errors in the browser.