In Python GUI development, how do I expose a REST API?

In Python GUI development, exposing a REST API can be done using a variety of libraries. One popular option is to use Flask to create a simple API. Below is an example that demonstrates how to set up a REST API with Flask.

from flask import Flask, jsonify, request app = Flask(__name__) # Sample data data = [ {'id': 1, 'name': 'Item 1'}, {'id': 2, 'name': 'Item 2'} ] @app.route('/api/items', methods=['GET']) def get_items(): return jsonify(data) @app.route('/api/items/', methods=['GET']) def get_item(item_id): item = next((item for item in data if item['id'] == item_id), None) return jsonify(item) if item else ('', 404) if __name__ == '__main__': app.run(debug=True)

Python Flask REST API GUI Development Web Development API Integration