mirror of
https://github.com/jlengrand/twitterboard.git
synced 2026-03-10 08:51:22 +00:00
In the process of adding the list of current active hashtags on web page
Creating a table in javascript is not a good way, as it is not rendered correctly in the browser. Better format it server side or find a way to populate it correctly client side. Also has to create a remove_hashtag method that takes arguments in input
This commit is contained in:
@@ -3,6 +3,8 @@ from flask import Flask, jsonify, render_template, request
|
||||
from sqlalchemy import create_engine
|
||||
from sqlalchemy.orm import sessionmaker
|
||||
from sqlalchemy import func
|
||||
from sqlalchemy import desc
|
||||
|
||||
|
||||
import data
|
||||
from datamodel import TrendyHashtag
|
||||
@@ -17,6 +19,7 @@ h = HashtagLogger(data.engine_url, oauth=data.oauth)
|
||||
h.start()
|
||||
|
||||
|
||||
## Utilities
|
||||
def connect():
|
||||
"""
|
||||
Separated so that the method can be run in each created thread.
|
||||
@@ -31,6 +34,28 @@ def connect():
|
||||
return Session(), engine # Bridges class to db
|
||||
|
||||
|
||||
## Ajax queries
|
||||
@app.route('/trendy')
|
||||
def trendy():
|
||||
|
||||
session, engine = connect()
|
||||
|
||||
# requests active hashtags
|
||||
h_query = session.query(TrendyHashtag).filter(TrendyHashtag.active == True).order_by(desc(TrendyHashtag.created))
|
||||
hashtags = h_query.all()
|
||||
|
||||
trendy = []
|
||||
for h in hashtags:
|
||||
trendy.append([h.hashtag, 1])
|
||||
|
||||
session.close()
|
||||
engine.dispose()
|
||||
|
||||
trendy = [[1, 2], [3, 4], [5, 6]]
|
||||
#trendy = 'plop'
|
||||
return jsonify(trendy=trendy)
|
||||
|
||||
|
||||
@app.route('/nb_trendy')
|
||||
def nb_trendy():
|
||||
|
||||
@@ -73,6 +98,7 @@ def stop():
|
||||
return "Session closed"
|
||||
|
||||
|
||||
## Static part
|
||||
@app.route('/')
|
||||
def index():
|
||||
return render_template('index.html')
|
||||
@@ -87,5 +113,6 @@ def statistics():
|
||||
def about():
|
||||
return render_template('about.html')
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
app.run(debug=True)
|
||||
|
||||
@@ -1 +1,28 @@
|
||||
I'm trendy!
|
||||
<script>
|
||||
(function worker() {
|
||||
$.ajax({
|
||||
url : $SCRIPT_ROOT + '/trendy',
|
||||
success: function(data) {
|
||||
console.log(data.trendy);
|
||||
var res = "<table>";
|
||||
var link = "<a href=\"/remove_hashtag/\">Remove</a>";
|
||||
for (i=0;i<data.trendy[0].length;i++){
|
||||
res = res + "<tr>";
|
||||
for (i=0;i<data.trendy.length;i++){
|
||||
res = res + "<td>" + data.trendy[i][0] + "</td>";
|
||||
res = res + "<td>" + link + "</td>";
|
||||
}
|
||||
res = res + "</tr>";
|
||||
}
|
||||
res = res + "</table>";
|
||||
|
||||
console.log(res);
|
||||
$('#trendy').html(res);
|
||||
},
|
||||
complete: function() {
|
||||
setTimeout(worker, 1000);
|
||||
}
|
||||
});
|
||||
})();
|
||||
</script>
|
||||
<span id='trendy'></span>
|
||||
1
twiderboard/templates/trendy_tmpl.html
Normal file
1
twiderboard/templates/trendy_tmpl.html
Normal file
@@ -0,0 +1 @@
|
||||
trendy_tmpl
|
||||
Reference in New Issue
Block a user