Created
July 10, 2014 10:32
-
-
Save arjun-menon/74fe776e82e781f66a4c to your computer and use it in GitHub Desktop.
Flask & SQLite3 experiment
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from flask import Flask, request | |
import sqlite3, time | |
app = Flask("Twitter clone") | |
def add_tweet(tweet): | |
moment = timeString = time.strftime("%Y-%m-%d %H:%M %p", time.localtime()) | |
c.execute("INSERT INTO tweets VALUES('Guest', ?, ?)", (tweet, moment)) | |
conn.commit() | |
@app.route('/', methods=['POST', 'GET']) | |
def page(): | |
conn = sqlite3.connect('tweets.db') | |
c = conn.cursor() | |
if request.method == "POST": | |
tweet = request.form['tweet'] | |
# add the new tweet | |
moment = timeString = time.strftime("%Y-%m-%d %H:%M %p", time.localtime()) | |
c.execute("INSERT INTO tweets VALUES('Guest', ?, ?)", (tweet, moment)) | |
conn.commit() | |
tweets = [(moment, tweet) for user, tweet, moment in c.execute("SELECT * FROM tweets WHERE user='Guest' ORDER BY moment")] | |
tweets.reverse() | |
content = "<html><head><title>Twitter clone</title></head>" | |
content += "<body><table border=1>" | |
content += "<tr><th>Moment</th><th>Tweet</th></tr>" | |
for tweet in tweets: | |
content += "<tr><td>%s</td><td>%s</td></tr>" % tweet | |
content += "</table></body></html>" | |
content += """ | |
<br /> | |
<form action="/" method="post"> | |
<input type="text" name="tweet" value=""/> | |
<input type="submit" value="Tweet!"/> | |
</form> | |
""" | |
conn.close() | |
return content | |
if __name__ == "__main__": | |
app.debug = True | |
app.run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment