<h1>Application Setup<aclass="headerlink"href="#application-setup"title="Link to this heading">¶</a></h1>
<p>A Flask application is an instance of the <aclass="reference internal"href="../api.html#flask.Flask"title="flask.Flask"><codeclass="xref py py-class docutils literal notranslate"><spanclass="pre">Flask</span></code></a> class.
Everything about the application, such as configuration and URLs, will
be registered with this class.</p>
<p>The most straightforward way to create a Flask application is to create
a global <aclass="reference internal"href="../api.html#flask.Flask"title="flask.Flask"><codeclass="xref py py-class docutils literal notranslate"><spanclass="pre">Flask</span></code></a> instance directly at the top of your code, like
how the “Hello, World!” example did on the previous page. While this is
simple and useful in some cases, it can cause some tricky issues as the
project grows.</p>
<p>Instead of creating a <aclass="reference internal"href="../api.html#flask.Flask"title="flask.Flask"><codeclass="xref py py-class docutils literal notranslate"><spanclass="pre">Flask</span></code></a> instance globally, you will create
it inside a function. This function is known as the <em>application
factory</em>. Any configuration, registration, and other setup the
application needs will happen inside the function, then the application
will be returned.</p>
<sectionid="the-application-factory">
<h2>The Application Factory<aclass="headerlink"href="#the-application-factory"title="Link to this heading">¶</a></h2>
<p>It’s time to start coding! Create the <codeclass="docutils literal notranslate"><spanclass="pre">flaskr</span></code> directory and add the
<codeclass="docutils literal notranslate"><spanclass="pre">__init__.py</span></code> file. The <codeclass="docutils literal notranslate"><spanclass="pre">__init__.py</span></code> serves double duty: it will
contain the application factory, and it tells Python that the <codeclass="docutils literal notranslate"><spanclass="pre">flaskr</span></code>
<divclass="code-block-caption"><spanclass="caption-text"><codeclass="docutils literal notranslate"><spanclass="pre">flaskr/__init__.py</span></code></span><aclass="headerlink"href="#id1"title="Link to this code">¶</a></div>
<p><codeclass="docutils literal notranslate"><spanclass="pre">create_app</span></code> is the application factory function. You’ll add to it
later in the tutorial, but it already does a lot.</p>
<olclass="arabic simple">
<li><p><codeclass="docutils literal notranslate"><spanclass="pre">app</span><spanclass="pre">=</span><spanclass="pre">Flask(__name__,</span><spanclass="pre">instance_relative_config=True)</span></code> creates the
some default configuration that the app will use:</p>
<ulclass="simple">
<li><p><aclass="reference internal"href="../config.html#SECRET_KEY"title="SECRET_KEY"><codeclass="xref py py-data docutils literal notranslate"><spanclass="pre">SECRET_KEY</span></code></a> is used by Flask and extensions to keep data
safe. It’s set to <codeclass="docutils literal notranslate"><spanclass="pre">'dev'</span></code> to provide a convenient value
during development, but it should be overridden with a random
value when deploying.</p></li>
<li><p><codeclass="docutils literal notranslate"><spanclass="pre">DATABASE</span></code> is the path where the SQLite database file will be
saved. It’s under
<aclass="reference internal"href="../api.html#flask.Flask.instance_path"title="flask.Flask.instance_path"><codeclass="xref py py-attr docutils literal notranslate"><spanclass="pre">app.instance_path</span></code></a>, which is the
path that Flask has chosen for the instance folder. You’ll learn
more about the database in the next section.</p></li>
doesn’t create the instance folder automatically, but it needs to be
created because your project will create the SQLite database file
there.</p></li>
<li><p><aclass="reference internal"href="../api.html#flask.Flask.route"title="flask.Flask.route"><codeclass="xref py py-meth docutils literal notranslate"><spanclass="pre">@app.route()</span></code></a> creates a simple route so you can
see the application working before getting into the rest of the
tutorial. It creates a connection between the URL <codeclass="docutils literal notranslate"><spanclass="pre">/hello</span></code> and a
function that returns a response, the string <codeclass="docutils literal notranslate"><spanclass="pre">'Hello,</span><spanclass="pre">World!'</span></code> in
<h2>Run The Application<aclass="headerlink"href="#run-the-application"title="Link to this heading">¶</a></h2>
<p>Now you can run your application using the <codeclass="docutils literal notranslate"><spanclass="pre">flask</span></code> command. From the
terminal, tell Flask where to find your application, then run it in
debug mode. Remember, you should still be in the top-level
<codeclass="docutils literal notranslate"><spanclass="pre">flask-tutorial</span></code> directory, not the <codeclass="docutils literal notranslate"><spanclass="pre">flaskr</span></code> package.</p>
<p>Debug mode shows an interactive debugger whenever a page raises an
exception, and restarts the server whenever you make changes to the
code. You can leave it running and just reload the browser page as you
follow the tutorial.</p>
<divclass="highlight-text notranslate"><divclass="highlight"><pre><span></span>$ flask --app flaskr run --debug
<p>Visit <aclass="reference external"href="http://127.0.0.1:5000/hello">http://127.0.0.1:5000/hello</a> in a browser and you should see the
“Hello, World!” message. Congratulations, you’re now running your Flask
web application!</p>
<p>If another program is already using port 5000, you’ll see
<codeclass="docutils literal notranslate"><spanclass="pre">OSError:</span><spanclass="pre">[Errno</span><spanclass="pre">98]</span></code> or <codeclass="docutils literal notranslate"><spanclass="pre">OSError:</span><spanclass="pre">[WinError</span><spanclass="pre">10013]</span></code> when the
server tries to start. See <aclass="reference internal"href="../server.html#address-already-in-use"><spanclass="std std-ref">Address already in use</span></a> for how to
handle that.</p>
<p>Continue to <aclass="reference internal"href="database.html"><spanclass="doc">Define and Access the Database</span></a>.</p>