31
loading...
This website collects cookies to deliver better user experience
python
command as given below:$ python
Python 3.9.7 (tags/v3.9.7:1016ef3, Aug 30 2021, 20:19:38) [MSC v.1929 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>>
$ python -m venv env
env
. Now, we need to activate the environment using the command:$ . env/Scripts/activate
(env)
in your terminal.$ pip install Flask
$ pip freeze
# OUTPUT
click==8.0.3
colorama==0.4.4
Flask==2.0.2
itsdangerous==2.0.1
Jinja2==3.0.2
MarkupSafe==2.0.1
Werkzeug==2.0.2
$ pip freeze > requirements.txt
a __init__.py
file. So, let's create our core package first.$ mkdir core
__init__.py
file inside the core directory:$ cd core
$ touch __init__.py
$ cd ..
from flask import Flask
app = Flask(__name__)
app
of class Flask. We use the __name__
argument to indicate the app's module or package, so that Flask knows where to find other files such as templates. routes.py
file. But what goes inside that file? Inside the routes.py file, we add various routes for our application. The routes are the different URLs that the application implements. These routes are handled by Python functions called view functions. Each view function is mapped with a route using Python decorators. So, within the core package, create a routes.py
file and add the following content inside that:from core import app
@app.route('/')
def say_hello():
return "Hello World!"
say_hello()
which just returns a string "Hello World!". This function, or view function, is mapped with a URL "/" using @app.route decorator. It means, whenever someone visits the "/" route, say_hello()
method is responsible to handle the request.a __init__.py
file as:from flask import Flask
app = Flask( __name__ )
from core import routes # Add this line
main.py
file and add the following content:from core import app
if __name__ == ' __main__':
app.run(debug=True)
debug=True
, the server will automatically reload if code changes, and will show an interactive debugger in the browser if an error occurs during a request.python main.py
command:$ python main.py
* Serving Flask app 'core' (lazy loading)
* Environment: production
WARNING: This is a development server. Do not use it in a production deployment.
Use a production WSGI server instead.
* Debug mode: on
* Restarting with stat
* Debugger is active!
* Debugger PIN: 131-151-153
* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
config.py
file : This file lies outside the core package and contains various configurations such as DEBUG, or database details such as URI.core/models.py
: This is where the models are defined. A model is a representation of a database table in code./core/templates
: This is the directory where all HTML files will go./core/static
: This is where static files such as CSS and JavaScript files as well as images usually go.config.py
file, let's add the following content.class Config(object):
SECRET_KEY = 'guess-me'
DEBUG = False
TESTING = False
CSRF_ENABLED = True
class ProductionConfig(Config):
DEBUG = False
MAIL_DEBUG = False
class StagingConfig(Config):
DEVELOPMENT = True
DEBUG = True
class DevelopmentConfig(Config):
DEVELOPMENT = True
DEBUG = True
class TestingConfig(Config):
TESTING = True
a __init__.py
file as:from flask import Flask
from config import DevelopmentConfig # Add this line
app = Flask( __name__ )
app.config.from_object(DevelopmentConfig) # Add this line
from core import routes