Create a Python Web Application Using the Wolfram Language
Using a Single File with URLDispatcher...
Create an API
Create an APIFunction that returns the distance between two cities:
- QuantityMagnitude is easier for external programs to understand than the Quantity returned by TravelDistance.
Create the web form interface
Apply FormFunction to the parameters of the API to construct the form:
- FormFunction and APIFunction have the same signature, so contents for one can be used for the other.
Create the endpoints for the app
Use URLDispatcher to create multiple endpoints to access either the web form or the API directly:
Export the app files
Create a directory to store the app files:
Use Export to write the content to a file called dispatcher.wl:
Serve the application
Open the terminal window and navigate to the app folder location:
$ cd ~/Desktop/city-distance-app
Run the following command to serve the application:
$ python -m wolframwebengine dispatcher.wl
----------------------------------------------------------------------
Address http://localhost:18000/
File path/city-distance-app/dispatcher.wl
----------------------------------------------------------------------
(Press CTRL+C to quit)
Go to http://localhost:18000 and click the Use the Form hyperlink to be taken to the appropriate endpoint:
Use the application
Find the distance between Berlin and Paris:
Using Multiple Files in a Directory Layout...
Create an API
Create an APIFunction that returns the distance between two cities:
- QuantityMagnitude is easier for external programs to understand than the Quantity returned by TravelDistance.
Create the web form interface
Apply FormFunction to the parameters of the API to construct the form:
- FormFunction and APIFunction have the same signature, so contents for one can be used for the other.
Create the directories
Create a directory to store the app files:
Create a subdirectory for the API:
Create a subdirectory for the form:
Export the app files
Use Map to batch export the app files to their appropriate locations, including both the API and the form:
- All files with extensions .m, .mx, .wxf, .wl are evaluated in the kernel using GenerateHTTPResponse on the content of the file, so the index.wl expression must be wrapped with Delayed.
- File extensions outside of .m, .mx, .wxf, .wl are served as static content.
Serve the application
Open the terminal window and navigate to the app folder location:
$ cd ~/Desktop/city-distance-app
Run the following command to serve the application:
$ python -m wolframwebengine city-distance-app
----------------------------------------------------------------------
Address http://localhost:18000/
Folder path/city-distance-app
Index index.wl
----------------------------------------------------------------------
(Press CTRL+C to quit)
- If the request path corresponds to a directory on disk, the server will search for a file named index.wl in that directory. This convention can be changed with the --index option.
Got to http://localhost:18000 and click the Use the Form hyperlink to be taken to the appropriate endpoint:
Use the application
Find the distance between Berlin and Paris: