Deepanshu
Deepanshu

Reputation: 17

How to import function into main file in flask?

I am splitting my main.py file into multiple files. I have a main.py file and another user.py file. So I just want to import function of user.py into main.py file and @app.route will remain stay in main.py file. Is there any other option except Blueprint in Flask?

Any help would be appreciated?

#main.py

from flask import Flask
app = Flask(__name__)

from user import about

@app.route('/')
def hello():
   return "Hello"

@app.route('/about')
about()

This about() part is giving me syntax error


#user.py

def about():
   return "Hey There!"

Upvotes: 0

Views: 3935

Answers (1)

Nihal
Nihal

Reputation: 5334

you can do it like this. just define route but call the function inside it

#main.py
from user import about

from flask import Flask
app = Flask(__name__)

from user import about

@app.route('/')
def hello():
   return "Hello"

@app.route('/about')
def hey():
    return about()

Upvotes: 3

Related Questions