Flask Development Part 3: File Structure

Shawn Hymers
Apr 3 · 3 min read

This is Part 3 of a series of articles that will walk you through the basics of Flask Web Development by developing and hosting a simple CRUD application.

This series is split into 6 parts:

  1. A simple Hello World app

Part 3: Building a Scalable File Structure.

So far we just have one file for the backend python code and one file for the front end html code. As our app gets more complex and we add more code we will want to split these off into multiple files. We will also want to be able to add in images, javascript files, user uploads, and more.

Let’s go ahead and set up a more robust file structure.

This structure give us 3 main directories:

  1. Backend: Houses all our Python files that handle routing, CRUD actions, and form validation.

Within the backend directory there are 3 .py files and a sub-directory, also with 3 .py files.

  1. config.py: This will set up our app and our database.

Now let’s move our app configuration code to our config.py file and our routes to the index.py file.

First we will move this code to the config.py file.

from flask import Flask
app = Flask(__name__)

Next, we will add an argument to the line where we create the instance of the Flask class.

from flask import Flask
app = Flask(__name__, template_folder='../templates')

Since we are going to be putting our routes in our index.py file within the ‘backend’ directory this will tell flask where to look for our HTML templates within our new file structure.

Then we will move this code to the index.py file.

from config import app
from flask import Flask, render_template
@app.route('/')
@app.route('/index')
def hello_world():
return render_template('index.html')

Now our app.py file is just:

if __name__ == '__main__': app.run(host="localhost", port=8000)

Lastly, we will update our imports in our app.py file to access the new files we made.

import sys
sys.path.append('backend')
import index
from config import app
if __name__ == '__main__': app.run(host="localhost", port=8000)

Don’t worry about the specifics of the rest of the files yet. We will be adding to them throughout the rest of the series.

The source code for this part can be found at my GitHub here:

In the next post we will use this new file structure to configure a database for our app.

Analytics Vidhya

Analytics Vidhya is a community of Analytics and Data…

Sign up for Analytics Vidhya News Bytes

By Analytics Vidhya

Latest news from Analytics Vidhya on our Hackathons and some of our best articles! Take a look.

By signing up, you will create a Medium account if you don’t already have one. Review our Privacy Policy for more information about our privacy practices.

Check your inbox
Medium sent you an email at to complete your subscription.

Shawn Hymers

Written by

Analytics Vidhya

Analytics Vidhya is a community of Analytics and Data Science professionals. We are building the next-gen data science ecosystem https://www.analyticsvidhya.com

Shawn Hymers

Written by

Analytics Vidhya

Analytics Vidhya is a community of Analytics and Data Science professionals. We are building the next-gen data science ecosystem https://www.analyticsvidhya.com

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store