Path: csiph.com!news.swapon.de!eternal-september.org!feeder.eternal-september.org!border1.nntp.ams1.giganews.com!nntp.giganews.com!newsfeed.xs4all.nl!newsfeed7.news.xs4all.nl!news.tele.dk!news.tele.dk!small.news.tele.dk!newsgate.cistron.nl!newsgate.news.xs4all.nl!nzpost1.xs4all.net!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'static': 0.03; '"""': 0.05; 'finally:': 0.05; 'json': 0.05; 'python3': 0.05; '"__main__":': 0.07; '__name__': 0.07; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'rows': 0.09; 'sqlite': 0.09; 'url:localhost': 0.09; 'template': 0.11; 'def': 0.13; 'producing': 0.15; '2>/dev/null': 0.16; 'columns': 0.16; 'commandline': 0.16; 'contextlib': 0.16; 'fishing': 0.16; 'fly': 0.16; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:plane.gmane.org': 0.16; 'received:t-ipconnect.de': 0.16; 'sqlite3': 0.16; 'subject:server': 0.16; 'sunset': 0.16; 'url:jquery': 0.16; 'wrote:': 0.16; 'tree': 0.18; 'try:': 0.18; '(not': 0.20; 'assuming': 0.22; 'import': 0.24; 'script': 0.25; 'header:User-Agent:1': 0.26; 'header:X-Complaints-To:1': 0.26; 'skip:# 10': 0.27; 'yield': 0.27; 'cat': 0.29; 'integrating': 0.29; 'invoke': 0.29; 'code': 0.30; 'skip:s 30': 0.31; 'implement': 0.32; 'table': 0.32; '[1]': 0.32; 'point': 0.33; 'traditional': 0.33; 'this?': 0.34; 'files,': 0.35; 'something': 0.35; 'but': 0.36; 'there': 0.36; 'serve': 0.36; 'url:non-standard http port': 0.36; 'to:addr:python-list': 0.36; 'subject:: ': 0.37; 'received:org': 0.37; 'end': 0.39; 'google': 0.39; 'data': 0.39; 'application': 0.39; 'to:addr:python.org': 0.40; 'received:de': 0.40; 'some': 0.40; 'your': 0.60; 'matter': 0.63; 'information': 0.63; 'album': 0.66; 'below.': 0.66; 'useful.': 0.72; 'bottle': 0.84; 'cecil': 0.84; 'curl': 0.84; 'flower': 0.84; 'much)': 0.84; 'url:api': 0.84; 'url:cecilwesterhof': 0.84; 'url:collection': 0.84; 'url:links': 0.84; 'url:posts': 0.84; 'westerhof': 0.84; 'fish': 0.91 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Peter Otten <__peter__@web.de> Subject: Re: SQLite3 and web server Date: Fri, 21 Aug 2015 11:32:51 +0200 Organization: None References: <87pp2hp3vg.fsf@Equus.decebal.nl> Mime-Version: 1.0 Content-Type: text/plain; charset="ISO-8859-1" Content-Transfer-Encoding: 7Bit X-Gmane-NNTP-Posting-Host: p57bd9370.dip0.t-ipconnect.de User-Agent: KNode/4.13.3 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.20+ Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 145 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1440149603 news.xs4all.nl 23746 [2001:888:2000:d::a6]:60761 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:95519 Cecil Westerhof wrote: > At the moment I serve a AngularJS web application with: > python3 -m http-server > > This only servers static html pages with the data contained in js > files, like: > $scope.links = [ > { desc: 'Album', url: > { 'https://plus.google.com/collection/MuwPX' > { }, desc: 'Heron Sunbathing', url: > I would like to retrieve the information out a SQLite3 database. I did > some Googling, but until now I did not find something useful. How > would I implement this? I like bottle for (not much) dabbling around with generated html. Assuming there is an sqlite database "links.sqlite" with a table "links" featuring columns "desc" and "url" you get a traditional html page built on the fly from the data in that table with the code shown below. Producing JSON is also demonstrated. Integrating the JSON data is a matter of the javascript framework; google found http://api.jquery.com/jquery.getjson/. $ cat serve.py #!/usr/bin/env python3 import bottle import json import sqlite3 from contextlib import contextmanager @contextmanager def open_db(): db = sqlite3.connect("links.sqlite") cs = db.cursor() try: yield cs finally: db.close() def read_links(): with open_db() as cs: return cs.execute("select desc, url from links;").fetchall() TEMPLATE = """ "Links as found in the db % for desc, url in rows: {{desc}}
% end """ @bottle.route("/links") def links_as_html(): rows = read_links() return bottle.template(TEMPLATE, rows=rows) @bottle.route("/links/data") def links_as_json(): bottle.response.content_type = "application/json" return json.dumps([ {"desc": desc, "url": url} for desc, url in read_links()], indent=4) if __name__ == "__main__": bottle.run(host="localhost", port=8080) [Instead of the following commandline gymnastics you can just invoke the script with $ python3 serve.py and then point your browser to http://localhost:8080/links] $ python3 serve.py 2>/dev/null & [1] 8418 $ curl http://localhost:8080/links 2>/dev/null | head "Links as found in the db Album
Heron Sunbathing
Heron Fishing
Water Lily
Tree at Pond
Fish
Fountain
Digitalis
Sunset
Digitalis 2
Water Lilies
Flower
Waterfalls
Frogs
$ curl http://localhost:8080/links/data 2>/dev/null | head [ { "desc": "Album", "url": "https://plus.google.com/collection/MuwPX" }, { "desc": "Heron Sunbathing", "url": "https://plus.google.com/+CecilWesterhof/posts/bHvSzBGobEj" }, { "desc": "Heron Fishing", "url": "https://plus.google.com/+CecilWesterhof/posts/TY3asc5oCnB" }, { "desc": "Water Lily", "url": "https://plus.google.com/+CecilWesterhof/posts/AtTwhL8SdnH" }, { "desc": "Tree at Pond", "url": "https://plus.google.com/+CecilWesterhof/posts/TyiZbUWdnrm"