Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #95519
| 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 | <python-python-list@m.gmane.org> |
| 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 <python-list.python.org> |
| List-Unsubscribe | <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe> |
| List-Archive | <http://mail.python.org/pipermail/python-list/> |
| List-Post | <mailto:python-list@python.org> |
| List-Help | <mailto:python-list-request@python.org?subject=help> |
| List-Subscribe | <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.7.1440149603.13558.python-list@python.org> (permalink) |
| 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 |
Show key headers only | View raw
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 = """
<html>
<head>
<title>"Links as found in the db</title>
</head>
<body>
% for desc, url in rows:
<a href={{url}}>{{desc}}</a><br/>
% end
</body></html>
"""
@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
<html>
<head>
<title>"Links as found in the db</title>
</head>
<body>
<a href=https://plus.google.com/collection/MuwPX>Album</a><br/>
<a href=https://plus.google.com/+CecilWesterhof/posts/bHvSzBGobEj>Heron
Sunbathing</a><br/>
<a href=https://plus.google.com/+CecilWesterhof/posts/TY3asc5oCnB>Heron
Fishing</a><br/>
<a href=https://plus.google.com/+CecilWesterhof/posts/AtTwhL8SdnH>Water
Lily</a><br/>
<a href=https://plus.google.com/+CecilWesterhof/posts/TyiZbUWdnrm>Tree
at Pond</a><br/>
<a
href=https://plus.google.com/+CecilWesterhof/posts/MoQ7vXs8HqP>Fish</a><br/>
<a
href=https://plus.google.com/+CecilWesterhof/posts/BDYkPKSMUwZ>Fountain</a><br/>
<a
href=https://plus.google.com/+CecilWesterhof/posts/ed3ZGNzb8kM>Digitalis</a><br/>
<a
href=https://plus.google.com/+CecilWesterhof/posts/DPbHHSFXBY4>Sunset</a><br/>
<a
href=https://plus.google.com/+CecilWesterhof/posts/ZZtSUwNb6RC>Digitalis
2</a><br/>
<a href=https://plus.google.com/+CecilWesterhof/posts/LY62DqLEJhG>Water
Lilies</a><br/>
<a
href=https://plus.google.com/+CecilWesterhof/posts/XFKyTcoakcy>Flower</a><br/>
<a
href=https://plus.google.com/+CecilWesterhof/posts/bfg5irDAn2T>Waterfalls</a><br/>
<a
href=https://plus.google.com/+CecilWesterhof/posts/jKr5B6EQyo1>Frogs</a><br/>
$ 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"
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
SQLite3 and web server Cecil Westerhof <Cecil@decebal.nl> - 2015-08-21 08:11 +0200
Re: SQLite3 and web server Steven D'Aprano <steve@pearwood.info> - 2015-08-21 16:50 +1000
Re: SQLite3 and web server Cecil Westerhof <Cecil@decebal.nl> - 2015-08-21 09:13 +0200
Re: SQLite3 and web server Chris Angelico <rosuav@gmail.com> - 2015-08-21 17:27 +1000
Re: SQLite3 and web server Cecil Westerhof <Cecil@decebal.nl> - 2015-08-21 11:44 +0200
Re: SQLite3 and web server Michael Torrie <torriem@gmail.com> - 2015-08-21 08:19 -0600
Re: SQLite3 and web server Cecil Westerhof <Cecil@decebal.nl> - 2015-08-21 17:23 +0200
Re: SQLite3 and web server Michael Torrie <torriem@gmail.com> - 2015-08-21 08:57 -0600
Re: SQLite3 and web server Cecil Westerhof <Cecil@decebal.nl> - 2015-08-22 13:38 +0200
Re: SQLite3 and web server Peter Otten <__peter__@web.de> - 2015-08-21 11:32 +0200
Re: SQLite3 and web server Cecil Westerhof <Cecil@decebal.nl> - 2015-08-21 17:12 +0200
Re: SQLite3 and web server Peter Otten <__peter__@web.de> - 2015-08-22 09:51 +0200
Re: SQLite3 and web server Cecil Westerhof <Cecil@decebal.nl> - 2015-08-22 13:41 +0200
Re: SQLite3 and web server Peter Otten <__peter__@web.de> - 2015-08-22 14:23 +0200
csiph-web