DelphiLearn PythonPythonPython GUIRAD Studio

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.

0_rundemo1-3322988
Open Demo01.dproj

 

1. What is REST System?

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

  • Client-Server: There should be a separation between the server that offers a service, and the client that consumes it.
  • Stateless: Each request from a client must contain all the information required by the server to carry out the request. In other words, the server cannot store information provided by the client in one request and use it in another request.
  • Cacheable: The server must indicate to the client if requests can be cached or not.
  • Layered System: Communication between a client and a server should be standardized in such a way that allows intermediaries to respond to requests instead of the end server, without the client having to do anything different.
  • Uniform Interface: The method of communication between a client and a server must be uniform.
  • Code on demand: Servers can provide executable code or scripts for clients to execute in their context. This constraint is the only one that is optional.

 

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: 

Installation progress in Windows Command Prompt:

1_pipinstallflask-6417742
pip install flask in Windows Command Prompt.

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

Output in Python GUI:

2-2663728

Result in browser:

2_2-7442900

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.

close
Related posts
DelphiDelphiVCLPythonPython GUIWindows

What is DelphiVCL.Application.CreateHandle?

DelphiDelphiVCLPythonPython GUIWindows

Creating A New Application With DelphiVCL.Application.Create

DelphiDelphiVCLPythonPython GUIWindows

All You Need To Know About Application.DialogHandle

DelphiDelphiVCLPythonPython GUIWindows

What is the FieldAddress Property in DelphiVCL.Application?

Schreiben Sie einen Kommentar

Ihre E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert.

de_DE_formalGerman