Deploying Flask(Python Rest) in IIS

Deploying Flask(Python Rest) in IIS

Getting started

Import dependency Pipe install flask

Sample script

from flask import Flask, url_for ,render_template, jsonify, request import config import requests #import arcgis import os, getpass

app = Flask(__name__) app.debug = True

class PrefixMiddleware(object): #class for URL sorting

def __init__(self, app, prefix=''): self.app = app self.prefix = prefix

def __call__(self, environ, start_response):

#in this line I'm doing a replace of the word pythonflaskredirectwhich is my app name in IIS to ensure proper URL redirect

if environ['PATH_INFO'].lower().replace(' /pythonflaskredirect','').startswith(self.prefix):

environ['PATH_INFO'] = environ['PATH_INFO'].lower().replace ('/pythonflaskredirect','')[len(self.prefix):]

environ['SCRIPT_NAME'] = self.prefix return self.app(environ, start_response) else: start_response('404', [('Content-Type', 'text/plain')]) return ["This url does not belong to the app.".encode()]

app.wsgi_app = PrefixMiddleware(app.wsgi_app, prefix='/api')

@app.route('/bar') def bar():

return "The URL for this page is {}".format(url_for('bar'))

@app.route('/v1/test/get/response', methods=['GET']) def api_all():

response = [ {'id': 0, 'desc': 'xxxx'}, {'id': 1, 'desc': 'xxxx'}

] return jsonify(response)

@app.route('/v1/export', methods=['GET']) def api_export():

args = request.args token = args['token']

query = " /services/vicfeaturelayer/FeatureServer/0/query?f=json&where=1% 3D1&returnGeometry=false&spatialRel=esriSpatialRelIntersects&outFields=* &orderByFields=OBJECTID% 20ASC&resultOffset=0&resultRecordCount=50&cacheHint=true&token=" + token

results =requests.get(query) return results.json()

if __name__ == '__main__': #print(getpass.getuser()) app.run(host='0.0.0.0',port=9010)

Running python in IIS

Below is the guide I used: This is something I have been meaning to try, running python code with arcpy via rest API. This can be extremely helpful when working with web hooks in survey123. The followings are records of steps I took to achieve this. Open command prompt as Administrator: Pip install wfastcgi (I ran pip from the arcgis pro script folder)

Enable wfastcgi (I ran wfastcgi-enable from the arcgis pro script folder)

Enable CGI on IIS

Confirming FastCGI on IIS Create application on default site and add the following to the web.config (if there isn't one, create one)

Set read/write/execute to py C:\Users\vtay\Documents\vstuff\PythonEnv\ws_usage_matrix\arcgispro-py3-elasticsearch Set read/write/execute to py Flask application To be able to import arcpy, the pool identity have to be set or given access to ArcGIS Pro (I provide access to ArcGIS pro on the root level but it wasn't enough, changing the identity pool to myself works however)

Alternatively, you may also set permission to the following python environment (Python.exe root dir) your python application ArcGIS Pro directory

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download