0

In this link http://flask.pocoo.org/docs/0.12/tutorial/setup/#tutorial-setup

In file in flaskr.py :

app = Flask(__name__) # create the application instance :)
app.config.from_object(__name__) # load config from this file , flaskr.py

# Load default config and override config from an environment variable
app.config.update(dict(
  DATABASE=os.path.join(app.root_path, 'flaskr.db'),
  SECRET_KEY='development key',
  USERNAME='admin',
  PASSWORD='default'
))
app.config.from_envvar('FLASKR_SETTINGS', silent=True)

The explanation for the line

DATABASE=os.path.join(app.root_path, 'flaskr.db'),

is :

Operating systems know the concept of a current working directory for each process. Unfortunately, you cannot depend on this in web applications because you might have more than one application in the same process.

For this reason the app.root_path attribute can be used to get the path to the application. Together with the os.path module, files can then easily be found. In this example, we place the database right next to it.

Can anyone explain it with example as I am unable to understand the explanation?

Shashank Singh
  • 647
  • 1
  • 5
  • 22
  • You can see information about depending on the current working directory in web applications in the mod_wsgi documentation at http://modwsgi.readthedocs.io/en/develop/user-guides/application-issues.html#application-working-directory – Graham Dumpleton Dec 26 '17 at 08:58

1 Answers1

0

I understand it as that: In a normal program, you would first do os.chdir(PATH) and then just open flaskr.db. But, as Flask starts several application (aka app) in a process, each application in a separate thread, but they all share the same "current directory" as the current directory is per-process and not per-thread. In order to be save you should always work with absolute directories and use app.root_path.

hansaplast
  • 11,007
  • 2
  • 61
  • 75