Skip to content

Instantly share code, notes, and snippets.

@abdelouahabb
Last active November 7, 2019 11:44
Show Gist options
  • Save abdelouahabb/01c7c88fed65d119d6b8 to your computer and use it in GitHub Desktop.
Save abdelouahabb/01c7c88fed65d119d6b8 to your computer and use it in GitHub Desktop.

Revisions

  1. abdelouahabb revised this gist Jun 21, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion handlers.py
    Original file line number Diff line number Diff line change
    @@ -30,7 +30,7 @@ class Getter(tornado.web.RequestHandler):
    def get(self):
    c.execute('select * from users')
    res = c.fetchall()
    #fetchmany(int), fetchone()
    #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)
  2. abdelouahabb revised this gist Jun 21, 2014. 2 changed files with 25 additions and 7 deletions.
    23 changes: 17 additions & 6 deletions handlers.py
    Original file line number Diff line number Diff line change
    @@ -7,21 +7,32 @@
    c = conn.cursor()

    try:
    c.execute('''CREATE TABLE users (nom text)''')
    c.execute('''CREATE TABLE users (nom text, prenom text, age int)''')
    except sqlite3.OperationalError:
    pass
    pass


    class BaseHandler(tornado.web.RequestHandler):
    def get(self):
    self.render('page.html')


    class Recup(tornado.web.RequestHandler):
    class Saver(tornado.web.RequestHandler):
    def post(self):
    nomm = self.get_argument('nom')
    print nomm
    c.execute("insert into users values (?)", (nomm,))
    pren = self.get_argument('prenom')
    age = int(self.get_argument('age'))
    c.execute("insert into users values (?, ?, ?)", (nomm, pren, age))
    conn.commit()
    conn.close()
    self.redirect('/success')

    class Getter(tornado.web.RequestHandler):
    def get(self):
    c.execute('select * from users')
    res = c.fetchall()
    #fetchmany(int), fetchone()
    resultat = []
    for i in res:
    resultat.append(i)
    self.write(str(resultat))

    9 changes: 8 additions & 1 deletion page.html
    Original file line number Diff line number Diff line change
    @@ -8,9 +8,16 @@
    <body>
    <form action='/toz' method='post' >
    {% raw xsrf_form_html() %}
    Toz<input type='text' name='nom'>
    Nom
    <input type='text' name='nom'>
    Prenom
    <input type='text' name='prenom'>
    Age
    <input type="number" name='age'>
    <input type='submit'>

    </form>


    </body>
    </html>
  3. abdelouahabb revised this gist Jun 21, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion handlers.py
    Original file line number Diff line number Diff line change
    @@ -21,7 +21,7 @@ class Recup(tornado.web.RequestHandler):
    def post(self):
    nomm = self.get_argument('nom')
    print nomm
    c.execute("insert into users values (?)", (nomm))
    c.execute("insert into users values (?)", (nomm,))
    conn.commit()
    conn.close()

  4. abdelouahabb revised this gist Jun 21, 2014. 1 changed file with 0 additions and 1 deletion.
    1 change: 0 additions & 1 deletion app.py
    Original file line number Diff line number Diff line change
    @@ -4,7 +4,6 @@
    import tornado.web
    import tornado.httpserver
    import tornado.ioloop
    import tornado.options
    from tornado.options import define, options
    import handlers
    import os
  5. abdelouahabb created this gist Jun 21, 2014.
    35 changes: 35 additions & 0 deletions app.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,35 @@
    #!/usr/bin/env python
    #-*- coding:utf-8 -*-

    import tornado.web
    import tornado.httpserver
    import tornado.ioloop
    import tornado.options
    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()
    27 changes: 27 additions & 0 deletions handlers.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,27 @@
    #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)''')
    except sqlite3.OperationalError:
    pass


    class BaseHandler(tornado.web.RequestHandler):
    def get(self):
    self.render('page.html')


    class Recup(tornado.web.RequestHandler):
    def post(self):
    nomm = self.get_argument('nom')
    print nomm
    c.execute("insert into users values (?)", (nomm))
    conn.commit()
    conn.close()

    16 changes: 16 additions & 0 deletions page.html
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,16 @@
    <!DOCTYPE html>

    <html>
    <head>
    <title>Page Title</title>
    </head>

    <body>
    <form action='/toz' method='post' >
    {% raw xsrf_form_html() %}
    Toz<input type='text' name='nom'>
    <input type='submit'>

    </form>
    </body>
    </html>