Skip to content

Instantly share code, notes, and snippets.

Created October 2, 2012 16:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/3820711 to your computer and use it in GitHub Desktop.
Save anonymous/3820711 to your computer and use it in GitHub Desktop.
Demo app for subweb
#!/usr/bin/python
#-*- coding: UTF-8 -*-
"""
(c) 2011, 2012 - Copyright Pierre-Yves Chibon
Distributed under License GPLv2 or later
You can find a copy of this license on the website
http://www.gnu.org/licenses/gpl.html
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
MA 02110-1301, USA.
Small web application to start investigating QTL hotspot form QTL
analysis made using MapQTL.
This web-app allows you to upload the output from your MapQTL analysis,
set a number of parameter and retrieve basic information regarding QTL
hotspots.
"""
import string
import random
import flask
APP = flask.Flask(__name__)
def id_generator(size=6, chars=string.ascii_uppercase + string.digits):
return ''.join(random.choice(chars) for x in range(size))
@APP.route('/')
def index():
""" Main page.
Shows the different url available.
"""
return flask.Response(
"""
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
</head>
<body>
<h1>SubWeb</h1>
<p>Welcome to the front page of subweb the web-service server for subsurface</p>
<br />
<br />
Your options are:
<ul>
<li> Generate a new login, see: <a href="/newlogin">/newlogin</a></li>
<li> Upload a new dive, see: <a href="/dive/login/lat/long/name">/dive/login/lat/long/name</a></li>
<li> Download your dives, see<a href="/mydives/login">/mydives/login</a></li>
</ul>
</body>
</html>
""")
@APP.route('/newlogin')
def newlogin():
""" Shows the different url available. """
user_id = id_generator(30)
# Retrieve all the IDs from the database to avoid duplicates
database = []
if user_id in database:
flask.redirect(flask.url_for('newlogin'))
return flask.Response('{"user": "%s"}' % user_id)
@APP.route('/dive/<login>/<latitude>/<longitude>/<name>')
def add_dive(login, latitude, longitude, name):
""" Add the given dive to the database associated with the given
user ID. """
# Save the dive to the database.
return flask.Response(
"""
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
</head>
<body>
<h1>SubWeb</h1>
<p>Your dive has been saved:</p>
<table>
<tr>
<td>Name</td><td>{0}</td>
</tr>
<tr>
<td>Latitute</td><td>{1}</td>
</tr>
<tr>
<td>Longitude</td><td>{2}</td>
</tr>
</table>
</body>
</html>
""".format(name, latitude, longitude))
@APP.route('/mydives/<login>')
def my_dives(login):
""" Return a json serialisation of all the dives in the database
associated with this login.
"""
# Save the dive to the database.
return flask.Response(
"""{"user": "%s", "dives" : [ {
"name":"%s",
"latitude":"%s",
"longitude":"%s"
}, {
"name":"%s",
"latitude":"%s",
"longitude":"%s"
}
]
}
""" % (id_generator(30), 'Dive1', '12', '23', 'Dive2', '34', '45'))
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