Nilesh Shinde
3 min readNov 6, 2023

--

In this blog, we’ll explore how to use Flask with SQLite, a lightweight and easy-to-use database system, to create a simple web application. We’ll provide example code and explanations to help you get started.

Flask with SQLite: Building a Simple Web Application

What is Flask ?

Flask is a micro web framework for Python that makes it easy to create web applications. It is known for its simplicity and flexibility, making it an excellent choice for building small to medium-sized web projects.

What is SQLite?

SQLite is a self-contained, serverless, and zero-configuration database engine. It is perfect for small to medium-sized web applications and mobile apps, as it doesn't require a separate server or complex setup.

Setting up the Environment

Before you start, make sure you have Flask and SQLite installed. You can install Flask using pip:

pip install Flask

Python comes with SQLite, so you don't need to install it separately.

Creating a Simple To-Do List Web Application

Let's build a simple to-do list application with Flask and SQLite. We'll create a web interface for users to add, view, and delete tasks.

1. Initializing the Flask App

from flask import Flask, render_template, request, redirect, url_for
import sqlite3

app = Flask(__name__)

We import Flask and SQLite modules, create a Flask app instance, and initialize a connection to the SQLite database.

2. Creating a Database

def create_database():
conn = sqlite3.connect('todo.db')
c = conn.cursor()
c.execute('''CREATE TABLE IF NOT EXISTS tasks (id INTEGER PRIMARY KEY, task TEXT)''')
conn.commit()
conn.close()

This function creates a database file named `todo.db` and a table named `tasks` to store our to-do list items.

3. Defining Routes

@app.route('/')
def index():
conn = sqlite3.connect('todo.db')
c = conn.cursor()
c.execute('SELECT * FROM tasks')
tasks = c.fetchall()
conn.close()
return render_template('index.html', tasks=tasks)

@app.route('/add', methods=['POST'])
def add_task():
task = request.form['task']
conn = sqlite3.connect('todo.db')
c = conn.cursor()
c.execute('INSERT INTO tasks (task) VALUES (?)', (task,))
conn.commit()
conn.close()
return redirect(url_for('index'))

@app.route('/delete/<int:task_id>')
def delete_task(task_id):
conn = sqlite3.connect('todo.db')
c = conn.cursor()
c.execute('DELETE FROM tasks WHERE id = ?', (task_id,))
conn.commit()
conn.close()
return redirect(url_for('index'))

We define three routes: `/` for viewing tasks, `/add` for adding tasks, and `/delete/<int:task_id>` for deleting tasks.

4. Creating Templates

Create an `index.html` template in a `templates` folder:


<!DOCTYPE html>
<html>
<head>
<title>ToDo List</title>
</head>
<body>
<h1>ToDo List</h1>
<form method="post" action="/add">
<input type="text" name="task" placeholder="Add a task" required>
<button type="submit">Add</button>
</form>
<ul>
{% for task in tasks %}
<li>
{{ task[1] }}
<a href="/delete/{{ task[0] }}">Delete</a>
</li>
{% endfor %}
</ul>
</body>
</html>

This template renders the to-do list and provides a form for adding tasks.

Running the Application

Finally, run the application:


if __name__ == '__main__’:
create_database()
app.run(debug=True)

Conclusion

Flask and SQLite make it easy to build web applications with a backend database. In this blog, we created a simple to-do list application using Flask, SQLite, and HTML templates. You can expand on this project by adding features like task editing and due dates. Flask's simplicity and SQLite's lightweight nature make them a great combination for small-scale web projects. Happy coding!

--

--

Nilesh Shinde

Nilesh Shinde: full-stack dev. React,next.js, Flask, SQL, Mongobd,node.js, express.js.