Rectangle 27 0

Your browser can't (and know you how to) execute Python functions/modules. What you want is to make an AJAX request. Basically, you need to put a web server in front of your Python function and then return the result of the function, here your XML file, when a certain URL is called. There's a lot of lightweight web framework that should help you to setup a quick web server to do that. For instance, Flask. Here's an example, totally inspired from the homepage of Flask:

from flask import Flask
from yourmodule import function_that_return_xml
app = Flask(__name__)

@app.route("/")
def hello():
    xml = function_that_return_xml()
    # make fancy operations if you want
    return xml

if __name__ == "__main__":
    app.run()

Then, here you can call http://localhost:5000 (default address, put it online if you want other users to use it) to get your XML file.

So, basically I make this proxy web server using flask, that calls the main geoserver and gets the xml.Then make AJAX calls to this proxy webserver and get the xml file in javascript, for displaying in HTML???

Thanks a lot !!

Calling a local python script from javascript - Stack Overflow

javascript python html
Rectangle 27 0

If you need to handle all urls not found on server just create 404 hanlder:

@app.errorhandler(404)
def page_not_found(e):
    # your processing here
    return result

python - Flask URL Route: Route All other URLs to some function - Stac...

python google-app-engine url-routing flask
Rectangle 27 0

Why not just use a parameter that can potentially be empty, with a default value of None?

@app.route('/item/<int:appitemid>/')
@app.route('/item/<int:appitemid>/<path:anythingcanbehere>')
def show_item(appitemid, anythingcanbehere=None):

python - Flask URL Route: Route Several URLs to the same function - St...

python url-routing flask
Rectangle 27 0

Your code snippet is calling the hello function directly. That's why the function is running.

For Flask, you define the routes using decorator functions. When you navigate to a URL that matches the decorator, it will execute the decorated function. In this example, if I navigate to "/" on my webserver, the hello function will execute.

To actually run your server, you need to do the following from the command line (which can also be found in Flask's documentation.

$ export FLASK_APP=hello.py
$ flask run

Where hello.py is the name of the file containing your Python code. You should also remove the direct call to hello() in your file. Then open a browser and navigate to http://localhost:5000/ to see the result.

Decorators are functions that wrap other functions in an attempt to alter the behavior of the sub-function. You can read the very detailed explanation in the Python wiki.

Decorators take in a function as their argument. Typically, decorators run some code before executing the sub-function. For example, if you want to add authentication to certain endpoints of your Flask app, you can use decorators. The decorators check to make sure a user is authenticated to use a resource before the actual code of that resource executes.

@authenticate
def hello():
   return "Hello world"

The authenticate function will run first. And then if the user is authenticated, it will call hello to execute the rest, otherwise, it will send an error back to the user. In the case of Flask's decorators, they are checking to see if an incoming request matches the route you've specified. If it does, then it executes the function. Otherwise, it checks the next route.

how do decorated functions work in flask/python? (app.route) - Stack O...

python flask decorator
Rectangle 27 0

from flask import Flask

app = Flask(__name__)

@app.route('/')
def index():
    return 'This is the front page'

@app.route('/hello/')
def hello():
    return 'This catches /hello'

@app.route('/<path:dummy>')
def fallback(dummy):
    return 'This one catches everything else'
path

python - Flask URL Route: Route All other URLs to some function - Stac...

python google-app-engine url-routing flask
Rectangle 27 0

Yes - you use the following construct:

@app.route('/item/<int:appitemid>/<path:path>')
@app.route('/item/<int:appitemid>', defaults={'path': ''})

You've actually implemented that backwards - the defaults bit should be on the shorter url.

@amber Ooops - thanks dropped the ball there - thanks for pointing that out

python - Flask URL Route: Route Several URLs to the same function - St...

python url-routing flask
Rectangle 27 0

I am not sure but could it be, that the url /data is used somewhere else and you never call this specific function. Can you add some logging or prints in the function to verify that the function is called. I tried your your code with python 2.7 and Flask 0.12 in a simple example and it works.

# -*- coding: utf-8 -*-

import json
from flask import Flask, jsonify, Response, request

# Initialize the Flask application
app = Flask(__name__)

# Set some configuration to the flask ap
app.config['DEBUG'] = True


# Default route for /
@app.route("/data", methods=['POST'])
def index():
    if request.method == "POST":
        result = {
            "data": {
                "name": "First, Last",
                "value": 1
            }
        }
        # Both worked 
        # return jsonify(result)
        return Response(json.dumps(result), mimetype='application/json; charset=utf-8')
    else:
        return abort(404)
export FLASK_APP=server.py
flask run

python - Flask: Post Response(json.dumps(dict)) not returning json - S...

python flask