Defining a different route for python app


Question

Here is my python code

    import os
from flask import Flask

app = Flask(__name__)

@app.route('/')
def hello():
    return 'Hello World!'
def Fun():
    return 'Having Fun!'

if __name__ == '__main__':
    # Bind to PORT if defined, otherwise default to 5000.
    port = int(os.environ.get('PORT', 5000))
    app.run(host='0.0.0.0', port=port)

On loading the www.example.com/ it goes to the hello function and returns Hello World! How to point a url like www.example.com/Fun to another function Fun

Update(Working Code)

Updated code but still not working

import os
from flask import Flask

app = Flask(__name__)

@app.route('/')

def hello():
    return 'Hello World --  jay!'

@app.route('/fun')
def fun():
    return 'have func!'

if __name__ == '__main__':
    # Bind to PORT if defined, otherwise default to 5000.
    port = int(os.environ.get('PORT', 5000))
    app.run(host='0.0.0.0', port=port)
1
1
1/15/2013 10:19:54 AM

Accepted Answer

Decorator @app.route sets url for your controller. So this code should work:

@app.route('/fun')
def fun():
    return 'Having Fun!'
3
1/15/2013 9:52:51 AM

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon