SwiftPythonWeb

A Swift/Vapor based web app using a Python script for processing which is used via PythonKit. For conversions between Python and Swift types, see there.

It present a very simple HTML interface to do the following:

  • Upload a (text) file given by the user.
  • Use a Python script to replace all “a” in the file by “x”.
  • Present a download link to download the result.

The app should be startet with the project diretory as the current directory. If you start the web app from within Xcode, be sure to set the project directory as custom working directory in the according Xcode scheme.

This web app then expects the following directories at the side of the project directory:

  • The directory python-scripts with the script script1.py as given below.
  • The directory upload to hold the uploaded files.

The script script1.py:

def replaceAllAs(text):
  return text.replace("a", "x")

The download directory is in Public within the project directory. Note that there is no cleaning up of uploaded or downloadable files.

The port is 9999 by default and can be set via e.g. --port 7777. See processArguments() in configure.swift for how to introduce other arguments if needed.

To use PythonKit on Linux, you have to set the environment variable PYTHON_LIBRARY, e.g.:

export PYTHON_LIBRARY=/usr/lib/python3.6/config-3.6m-x86_64-linux-gnu/libpython3.6.so

Use find /usr/lib -name *libpython*.so to search for an appropriate Python library.

GitHub

View Github