-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.py
160 lines (96 loc) · 3.78 KB
/
server.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
from jinja2 import StrictUndefined
from flask import Flask, jsonify, render_template, redirect, request, flash, session
from flask_debugtoolbar import DebugToolbarExtension
from werkzeug.contrib.cache import SimpleCache
from lda import perform_lda
import queries
import os
from model import connect_to_db, db
from model import Poem, Author, Subject
cache = SimpleCache()
app = Flask(__name__)
# Raises an error if you use undefined Jinja variable.
app.jinja_env.undefined = StrictUndefined
@app.route('/')
def index():
"""Homepage."""
term = request.args.get('term')
if not term:
return render_template('homepage.html', poems=[], headlines=[], subjects=[], term='')
isalpha = term.isalpha()
if isalpha == False:
flash('Word must be all alpha characters.')
return redirect('/')
poems = Poem.query.options(db.joinedload('subjects')).filter(Poem.tsv.match(term)).all()
if not poems:
flash('Word not found. Try another!')
return redirect('/')
headlines = queries.get_headlines(term)
subjects = queries.get_subject_counts(poems)
return render_template("homepage.html", headlines=headlines, poems=poems, subjects=subjects, term=term)
@app.route('/authors')
def authors():
"""displays a list of poets"""
authors = cache.get('authors')
if authors is None:
authors = queries.get_authors()
cache.set('authors', authors, timeout=60 * 60)
return render_template('authors.html', authors=authors)
@app.route('/subjects')
def subjects():
"""explore by subjects"""
subjects = Subject.query.order_by('subject_name').all()
return render_template('subjects.html', subjects=subjects)
@app.route('/lda')
def explore_lda():
"""lda proof of concept page"""
authors = Author.query.all()
return render_template('lda.html', authors=authors)
@app.route('/kmeans')
def explore_kmeans():
"""kmeans proof of concept page"""
return render_template('kmeans.html')
@app.route('/about')
def about():
"""returns about page"""
return render_template('about.html')
##########################################################################################################
# unique id routes
@app.route('/author/<author_id>')
def specfic_author(author_id):
"""displays word stats for specific authors"""
author = queries.author_top_words(author_id)
return render_template('author.html', author=author)
@app.route('/poem/<poem_id>')
def specific_poem(poem_id):
"""displays a single poem"""
poem = Poem.query.get(poem_id)
db.session.commit()
return render_template('poem.html', poem=poem)
##########################################################################################################
# json routes
@app.route('/subject_info/<subject_id>.json')
def subject_info(subject_id):
"""get most common words for a given subject"""
top_words = queries.subject_top_words(subject_id)
return jsonify(top_words)
@app.route('/author_lda/<author_id>.json')
def author_lda(author_id):
"""get topic breakdown for particular author"""
lda = perform_lda(author_id)
return jsonify(lda)
if __name__ == "__main__":
# We have to set debug=True here, since it has to be True at the
# point that we invoke the DebugToolbarExtension
# app.debug = True
# app.config['SQLALCHEMY_ECHO'] = True
PORT = int(os.environ.get("PORT", 5000))
DEBUG = "NO_DEBUG" not in os.environ
SECRET_KEY = os.environ.get("FLASK_SECRET_KEY", "asdf9k$")
# Required to use Flask sessions and the debug toolbar
app.secret_key = SECRET_KEY
app.jinja_env.auto_reload = True
connect_to_db(app, os.environ.get("DATABASE_URL"))
# Use the DebugToolbar
# DebugToolbarExtension(app)
app.run(host='0.0.0.0', port=PORT, debug=DEBUG)