Archived
1
0
Fork 0
This commit is contained in:
Lukas Winkler 2016-10-05 14:25:28 +02:00
parent 59cbf3eb4d
commit 5c6c1a43fe
4 changed files with 92 additions and 5 deletions

View file

@ -14,6 +14,7 @@ from flask import send_from_directory
from flask import url_for
from config import database
import top
db = MySQLdb.connect(database["host"],
database["user"],
@ -28,7 +29,7 @@ app.config.update(
@app.route('/api/connection/', methods=["GET"])
def hello_world():
def getConnection():
if not request.args \
or 'from' not in request.args or 'to' not in request.args \
or not request.args["from"].isdigit() or not request.args["to"].isdigit():
@ -70,6 +71,14 @@ def get_connection(connection_id):
return jsonify(geojson)
@app.route('/api/top/', methods=["GET"])
def get_top():
if not request.args \
or 'type' not in request.args:
abort(400)
return jsonify(top.helloworld(cur, request.args["type"]))
@app.route('/')
def send_main_html():
return send_file('www/map.html')

35
top.py Normal file
View file

@ -0,0 +1,35 @@
topSQL = {
"shortestConnections": """
SELECT
id,
s.name,
g.name,
length
FROM connections
LEFT JOIN stationen AS s ON start = s.ref
LEFT JOIN stationen AS g ON goal = g.ref
WHERE start != goal
ORDER BY length ASC
LIMIT 10
""",
"farAway": """
SELECT *
FROM (SELECT
name,
length,
ref
FROM connections
LEFT JOIN stationen ON (start = ref OR goal = ref)
WHERE start != goal
ORDER BY length ASC) AS x
GROUP BY ref
ORDER BY length DESC"""
}
def helloworld(cursor, top):
cursor.execute(topSQL[top])
return cursor.fetchall()

View file

@ -6,6 +6,8 @@
<link rel="stylesheet" href="https://unpkg.com/leaflet@1.0.1/dist/leaflet.css"/>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/select2/4.0.3/css/select2.min.css"
integrity="sha256-xJOZHfpxLR/uhh1BwYFS5fhmOAdIRQaiOul5F/b7v3s=" crossorigin="anonymous">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/normalize/5.0.0/normalize.min.css"
integrity="sha256-t2/7smZfgrST4FS1DT0bs/KotCM74XlcqZN5Vu7xlrw=" crossorigin="anonymous"/>
<style>
body {
margin: 0;
@ -34,7 +36,6 @@
cursor: pointer;
}
</style>
</head>
<body>
<div id="map">
@ -224,7 +225,7 @@
return false;
}
console.info(fromId + "->" + toId);
$.getJSON("http://127.0.0.1:5000/api/connection/", {
$.getJSON("/api/connection/", {
from: fromId,
to: toId
}).done(function (data) {
@ -245,7 +246,6 @@
}
)
})
</script>
<script src="sidebar.js"></script>
</html>

43
www/sidebar.js Normal file
View file

@ -0,0 +1,43 @@
var sidebar = L.Control.extend({
options: {
position: 'bottomright'
},
onAdd: function (map) {
var container = L.DomUtil.create('div', 'leaflet-bar leaflet-control leaflet-control-custom');
container.style.backgroundColor = "white";
container.style.height = '600px';
container.style.width = '400px';
var table = L.DomUtil.create("table", "sidebarTable", container);
var button = document.createElement("button");
button.textContent = "Topliste";
container.appendChild(button);
return container;
}
});
map.addControl(new sidebar);
$(".leaflet-control-custom button").on("click", function () {
loadSidebar();
});
function loadSidebar() {
$.getJSON("/api/top/", {
type: "shortestConnections"
}).done(function (data) {
var table = $(".sidebarTable");
for (var i = 0; i < data.length; i++) {
var line = document.createElement("tr");
var single = data[i];
console.log(single);
for (var j = 0; j < single.length; j++) {
var td = document.createElement("td");
td.textContent = single[j];
line.appendChild(td);
}
table.append(line);
}
});
}