Giter Site home page Giter Site logo

kingofspades / flasktodoappdemo Goto Github PK

View Code? Open in Web Editor NEW
0.0 1.0 0.0 51 KB

I was learning to work with Flask, SQLAlchemy and WTForms so I created this simple todo app as reference to my future self and starting point for new apps

Makefile 2.01% Python 47.44% HTML 50.56%

flasktodoappdemo's Introduction

I was learning to work with Flask, SQLAlchemy and WTForms so I created this simple todo app as reference to my future self and starting point for new apps. This example can be easily expanded to add new functions.

Setting up initial install

Let's start by cloning this repo:

$ git clone https://github.com/KingOfSpades/flaskTodoAppDemo.git
$ cd flaskTodoAppDemo

As always you should start with an empty venv, activate it and install the required dependancys:

$ python3 -m venv .venv
$ source .venv/bin/activate
$ pip install -r requirements.txt

Now you can fill initalize the database with some dummy data by using make:

$ make database

This will create the inital database we need with some sample todo's. Have a look in flaskr/initDB.py if you want to see what's going on.

Now start the app with make or run it manually:

$ make run

This will start the app on: http://127.0.0.1:5000. You should now see this beautifull todo app:

Example of todo app running

Explaining the app

This is easy app tha relies on one (1) database model, a FlaskForm and four (4) routes to handle requests:

  • / route handels returinging the main page and index
  • /add route is used to add new todo's
  • /update route is used to update a todo based on the provided id. In our case it togles the done status of a todo
  • /delete route deletes a todo based on the provide id

Diagram overview

flowchart LR
    browser --> / --> |return| render(index)
    browser --> /add --> add --> |redirect| /
    browser --> /update/int:id --> update --> |redirect| /
    browser --> /delete/int:id --> delete --> |redirect| /
    render ---> |shows application| browser
    db --> |provides data| render

    
   add -.-> |add| db[(Database)]
   update -.-> |update| db[(Database)]
   delete -.-> |delete| db[(Database)]

Forms

The app uses a single form linked to a class to generate the add form:

# flaskr/app.py
...
# Todo form for adding items
class TodoForm(FlaskForm):
  todo = StringField("Todo")
  submit = SubmitField("Add")
...

This form is then renderd in the index.html template:

# flaskr/templates/index.html
...
<form method="POST" action="/add" class="input-group">
  <div class="form-outline flex-fill">
  {{ template_form.hidden_tag() }}
    {{ template_form.todo(
      class_='form-control',
      placeholder='Add a new todo ...',
      required='required'
      ) 
    }}
    {{ template_form.submit(class_='btn btn-primary btn-sm  btn-block') }}
  </div>
</form>
...

Database

The database consists of a simple class to hold an id, todo_text and the state of the Todo (completed, True or False):

# flaskr/app.py
# Todo class/model for holding todo's
class Todo(db.Model):
  id = db.Column(db.Integer, primary_key = True)
  todo_text = db.Column(db.String(256), index = True)
  completed = db.Column(db.Boolean, unique=False, default=False)

Updating todo's

This is simply done by rendering the target URL + todo ID, for example:

/delete/{{ todo.id }}

flasktodoappdemo's People

Contributors

kingofspades avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.