Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,6 @@ Implement a ToDo app in Flask.
* Write view in separate file.
* Add another parameter in URL `num`, which indicate how many todos a user want to view.
* Make a pull request in original repository with screenshots.



Binary file added Screenshot from 2019-07-14 23-40-07.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Screenshot from 2019-07-14 23-41-14.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Screenshot from 2019-07-14 23-41-29.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Screenshot from 2019-07-14 23-41-40.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
55 changes: 36 additions & 19 deletions week-0/day-3/my_todo_app/todo_app/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,20 @@
from flask import request

from flask import render_template
from flaskext.mysql import MySQL

# our fake db
todo_store = {}
todo_store['depo'] = ['Go for run', 'Listen Rock Music']
todo_store['shivang'] = ['Read book', 'Play Fifa', 'Drink Coffee']
todo_store['raj'] = ['Study', 'Brush']
todo_store['sanket'] = ['Sleep', 'Code']
todo_store['aagam'] = ['play cricket', 'have tea']

def create_app(test_config=None):
# create and configure the app
app = Flask(__name__, instance_relative_config=True)
mysql = MySQL()

# MySQL configurations
app.config['MYSQL_DATABASE_USER'] = 'ayush'
app.config['MYSQL_DATABASE_PASSWORD'] = 'password'
app.config['MYSQL_DATABASE_DB'] = 'mydb'
app.config['MYSQL_DATABASE_HOST'] = 'localhost'
mysql.init_app(app)

# ensure the instance folder exists
try:
Expand All @@ -24,14 +26,17 @@ def create_app(test_config=None):
pass

def select_todos(name):
global todo_store
return todo_store[name]
conn = mysql.connect()
cursor =conn.cursor()
cursor.execute("SELECT * from user where name= %s",name)
data = cursor.fetchone()
return(data[2])

def insert_todo(name, todo):
global todo_store
current_todos = todo_store[name]
current_todos.append(todo)
todo_store[name] = current_todos
conn = mysql.connect()
cursor =conn.cursor()
cursor.execute("INSERT into user(name,todo) values (%s,%s)",(name,todo))
conn.commit()
return

def add_todo_by_name(name, todo):
Expand All @@ -40,10 +45,7 @@ def add_todo_by_name(name, todo):
return

def get_todos_by_name(name):
try:
return select_todos(name)
except:
return None
return select_todos(name)


# http://127.0.0.1:5000/todos?name=duster
Expand All @@ -53,13 +55,28 @@ def todos():
print('---------')
print(name)
print('---------')

person_todo_list = get_todos_by_name(name)
todo=person_todo_list.split(',')
if person_todo_list == None:
return render_template('404.html'), 404
else:
return render_template('todo_view.html',todos=person_todo_list)
return render_template('todo_view.html',todos=todo)

@app.route('/todos_by_num')
def todos_by_num():
name = request.args.get('name')
num=request.args.get('num')
num=int(num)
print('---------')
print(name)
print(type(num))
print('---------')
person_todo_list = get_todos_by_name(name)
todo=person_todo_list.split(',')
if person_todo_list == None:
return render_template('404.html'), 404
else:
return render_template('todo_view.html',todos=todo[:num])

@app.route('/add_todos')
def add_todos():
Expand Down
Binary file not shown.
3 changes: 2 additions & 1 deletion week-0/day-3/my_todo_app/todo_app/templates/todo_view.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
List of my todos:<br/>
<h1>
List of my todos:</h1><br/>

{% for todo in todos %}
{{todo}}<br/>
Expand Down
28 changes: 0 additions & 28 deletions week-0/day-3/setup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -31,34 +31,6 @@ touch __init__.py # __init__.py is just like main function in c/c++ program

# adding file content
echo "adding minimal code to run flask app"
cat > __init__.py << SERVER_SCRIPT
import os

from flask import Flask


def create_app(test_config=None):
# create and configure the app
app = Flask(__name__, instance_relative_config=True)

# ensure the instance folder exists
try:
os.makedirs(app.instance_path)
except OSError:
pass

# a simple page that list my todos
@app.route('/shivang')
def shivang():
return ('Wake Up' + '<br/>' +
'Drink Coffee' + '<br/>' +
'Read Non-fiction Novel' + '<br/>'
)

return app

SERVER_SCRIPT

# going back to parent directory
cd ../

Expand Down