Last active
November 7, 2019 11:44
-
-
Save abdelouahabb/01c7c88fed65d119d6b8 to your computer and use it in GitHub Desktop.
Using SQLite with Tornado. just a simple code, tornado is easy, and if you want to make a simple application that runs on the same server, use this Gist for less dependecies ^_^
This file contains hidden or 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
#!/usr/bin/env python | |
#-*- coding:utf-8 -*- | |
import tornado.web | |
import tornado.httpserver | |
import tornado.ioloop | |
from tornado.options import define, options | |
import handlers | |
import os | |
define("port", default=8000, type=int) | |
urls = [ | |
(r"/", handlers.BaseHandler), | |
(r"/toz", handlers.Recup), | |
(r"/(.*)", tornado.web.StaticFileHandler, {"path": r"{0}".format(os.path.dirname(__file__))}) | |
] | |
settings = dict({ | |
"template_path": os.path.join(os.path.dirname(__file__), "templates"), # create a folder templates and put the html file there | |
#"static_path": os.path.join(os.path.dirname(__file__), "static"), # if there is static files, uncomment this. | |
"cookie_secret": str(os.urandom(45)), | |
"xsrf_cookies": True, | |
"debug": False, | |
"gzip": True, | |
}) | |
application = tornado.web.Application(urls, **settings) | |
if __name__ == "__main__": | |
server = tornado.httpserver.HTTPServer(application) | |
server.listen(options.port) | |
tornado.ioloop.IOLoop.instance().start() |
This file contains hidden or 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
#coding: utf-8 | |
import tornado.web | |
import sqlite3 | |
conn = sqlite3.connect('c:/toz.db') | |
c = conn.cursor() | |
try: | |
c.execute('''CREATE TABLE users (nom text, prenom text, age int)''') | |
except sqlite3.OperationalError: | |
pass | |
class BaseHandler(tornado.web.RequestHandler): | |
def get(self): | |
self.render('page.html') | |
class Saver(tornado.web.RequestHandler): | |
def post(self): | |
nomm = self.get_argument('nom') | |
pren = self.get_argument('prenom') | |
age = int(self.get_argument('age')) | |
c.execute("insert into users values (?, ?, ?)", (nomm, pren, age)) | |
conn.commit() | |
self.redirect('/success') | |
class Getter(tornado.web.RequestHandler): | |
def get(self): | |
c.execute('select * from users') | |
res = c.fetchall() | |
#search about fetchmany(int), fetchone() to get only some records or one (note that after getting it, the cursor will increment) | |
resultat = [] | |
for i in res: | |
resultat.append(i) | |
self.write(str(resultat)) | |
This file contains hidden or 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
<!DOCTYPE html> | |
<html> | |
<head> | |
<title>Page Title</title> | |
</head> | |
<body> | |
<form action='/toz' method='post' > | |
{% raw xsrf_form_html() %} | |
Nom | |
<input type='text' name='nom'> | |
Prenom | |
<input type='text' name='prenom'> | |
Age | |
<input type="number" name='age'> | |
<input type='submit'> | |
</form> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
where's your Recup class?