Site icon Python GUI

Easily Create A Flexible Python REST Server In A Delphi Windows GUI App

blogbanner3 12

REST (REpresentational State Transfer) has emerged as the standard architectural design for web services and web APIs in these recent years.

This post will demonstrate how easy it is to create a RESTful web service using Python and the Flask microframework in Python4Delphi with RAD Studio and gets the results.

Prerequisites: Before we begin to work, download and install the latest Python for your platform. Follow the Python4Delphi installation instructions mentioned here. Alternatively, you can check out the easy instructions found in this video Getting started with Python4Delphi.

First, open and run our Python GUI using project Demo1 from Python4Delphi with RAD Studio. Then insert the script into the lower Memo, click the Execute button, and get the result in the upper Memo. You can find the Demo1 source on GitHub. The behind the scene details of how Delphi manages to run your Python code in this amazing Python GUI can be found at this link.

Open Demo01dproj

 

1. What is REST System?

The REST system is defined by these six characteristics as its design rules:

 

2. Introduction to RESTful Web Service

The REST architecture was originally designed to fit the HTTP protocol that the world wide web uses.

Central to the concept of RESTful web services is the notion of resources. Resources are represented by Uniform Resource Identifier or URIs. The clients send requests to these URIs using the methods defined by the HTTP protocol, and possibly as a result of that the state of the affected resource changes.

The HTTP request methods are typically designed to affect a given resource in standard ways:

HTTP MethodActionExamples
GETObtain information about a resourcehttp://example.com/api/orders
(retrieve order list)
GETObtain information about a resourcehttp://example.com/api/orders/123
(retrieve order #123)
POSTCreate a new resourcehttp://example.com/api/orders
(create a new order, from data provided with the request)
PUTUpdate a resourcehttp://example.com/api/orders/123
(update order #123, from data provided with the request)
DELETEDelete a resourcehttp://example.com/api/orders/123
(delete order #123)

The REST design does not require a specific format for the data provided with the requests. In general data is provided in the request body as a JSON blob, or sometimes as arguments in the query string portion of the URL.

 

3. A Simple Flask API GET Request

Firstly, make sure you have Flask installed. It’s easiest to use Python package manager, pip or easy install: 

[crayon-662b48f0c3d64016948705/]

Installation progress in Windows Command Prompt:

pip install flask in Windows Command Prompt

Now open up the Demo01 VCL and copy-paste these code:

[crayon-662b48f0c3d76870438094/]

Output in Python GUI:

Result in browser:

Congratulations! You have integrated what “seems complicated concept” Python REST Server with Delphi, using Python4Delphi.

Check out Python4Delphi which easily allows you to build Python GUIs for Windows using Delphi.

Exit mobile version