|
|
|
#!/usr/bin/env python3
|
|
|
|
|
|
|
|
import os, sys
|
|
|
|
from sys import stdout
|
|
|
|
import flask
|
|
|
|
from flask import request, redirect, send_from_directory
|
|
|
|
import tfidf
|
|
|
|
import readings
|
|
|
|
import printer
|
|
|
|
from manifestos import manifestos
|
|
|
|
|
|
|
|
|
|
|
|
# Create the application.
|
|
|
|
APP = flask.Flask(__name__)
|
|
|
|
|
|
|
|
# Jinja filters
|
|
|
|
def prettyfilename(filename):
|
|
|
|
return filename.replace('_', ' ').replace('-', ' ').replace('.txt', '')
|
|
|
|
APP.jinja_env.filters['prettyfilename'] = prettyfilename
|
|
|
|
|
|
|
|
# Jinja globals
|
|
|
|
def get_random(x, y):
|
|
|
|
from random import randint
|
|
|
|
return randint(x, y)
|
|
|
|
APP.jinja_env.globals.update(get_random=get_random)
|
|
|
|
|
|
|
|
@APP.route('/', methods=['GET', 'POST'])
|
|
|
|
def index():
|
|
|
|
return redirect('/fr/')
|
|
|
|
|
|
|
|
@APP.route('/<lang>/', methods=['GET', 'POST'])
|
|
|
|
def index_lang(lang):
|
|
|
|
"""
|
|
|
|
Displays the index page accessible at '/<lang>/'
|
|
|
|
Which is either the start page (index.html)
|
|
|
|
or a results page (results.html).
|
|
|
|
"""
|
|
|
|
query = None
|
|
|
|
results = None
|
|
|
|
|
|
|
|
query = request.args.get('q', '')
|
|
|
|
suggestions = open('words.txt', 'r').readlines()
|
|
|
|
|
|
|
|
# Check printer argument
|
|
|
|
if printer.connected == True:
|
|
|
|
connection = 'connected'
|
|
|
|
else:
|
|
|
|
connection = 'connected'
|
|
|
|
|
|
|
|
# Check if print command
|
|
|
|
if request.args.get('results_count', ''):
|
|
|
|
results_count = int(request.args.get('results_count', ''))
|
|
|
|
print('results_count', results_count)
|
|
|
|
else:
|
|
|
|
results_count = 0
|
|
|
|
|
|
|
|
if request.args.get('q', ''):
|
|
|
|
results, filenames, analytics = readings.request_results(query)
|
|
|
|
|
|
|
|
# print commands
|
|
|
|
if request.args.get('print', '') == 'now':
|
|
|
|
if connection == 'connected':
|
|
|
|
results_count = printer.printNow(query, results, results_count)
|
|
|
|
|
|
|
|
return flask.render_template(lang+'/results.html', query=query, results=results, filenames=filenames, connection=connection, suggestions=suggestions, analytics=analytics, lang=lang, results_count=results_count)
|
|
|
|
|
|
|
|
else:
|
|
|
|
index = readings.load_index()
|
|
|
|
filenames = [manifesto for manifesto, _ in index.items()]
|
|
|
|
suggestions = open('words.txt', 'r').readlines()
|
|
|
|
mappings_top = open('tfidf.top50.txt', 'r').readlines()
|
|
|
|
return flask.render_template(lang+'/index.html', filenames=filenames, suggestions=suggestions, mappings=mappings_top, lang=lang)
|
|
|
|
|
|
|
|
@APP.route('/<lang>/about', methods=['GET', 'POST'])
|
|
|
|
def about(lang):
|
|
|
|
"""
|
|
|
|
Displays the about page accessible at '/<lang>/about'.
|
|
|
|
"""
|
|
|
|
index = readings.load_index()
|
|
|
|
filenames = sorted([manifesto for manifesto, _ in index.items()])
|
|
|
|
return flask.render_template(lang+'/about.html', filenames=filenames, lang=lang)
|
|
|
|
|
|
|
|
@APP.route('/<lang>/manifesto/<name>', methods=['GET', 'POST'])
|
|
|
|
def manifesto(lang, name):
|
|
|
|
"""
|
|
|
|
Displays the page accessible at '/<lang>/manifesto/<name>'.
|
|
|
|
Here, an iframe is shown with the manifesto in its own context.
|
|
|
|
"""
|
|
|
|
index = readings.load_index()
|
|
|
|
filenames = sorted([manifesto for manifesto, _ in index.items()])
|
|
|
|
link = manifestos[name]
|
|
|
|
return flask.render_template(lang+'/manifesto.html', filenames=filenames, name=name, link=link, lang=lang)
|
|
|
|
|
|
|
|
@APP.route('/<lang>/mappings', methods=['GET', 'POST'])
|
|
|
|
def contrast_mappings(lang):
|
|
|
|
"""
|
|
|
|
Displays the page accessible at '/<lang>/mappings'.
|
|
|
|
A TF-IDF visualisation is displayed,
|
|
|
|
using the TF-IDF values as font-size.
|
|
|
|
"""
|
|
|
|
mappings, filenames = readings.request_mappings_all()
|
|
|
|
return flask.render_template(lang+'/mappings.html', filenames=filenames, mappings=mappings, lang=lang)
|
|
|
|
|
|
|
|
@APP.route('/<lang>/mappings/<name>', methods=['GET', 'POST'])
|
|
|
|
def contrast_mappings_name(lang, name):
|
|
|
|
"""
|
|
|
|
Displays the page accessible at '/<lang>/mappings/<name>'.
|
|
|
|
A TF-IDF visualisation is displayed from the specific manifesto, using the TF-IDF values as font-size.
|
|
|
|
"""
|
|
|
|
mappings, filenames = readings.request_mappings(name)
|
|
|
|
return flask.render_template(lang+'/mappings-name.html', filenames=filenames, mappings=mappings[name], manifesto=name, lang=lang)
|
|
|
|
|
|
|
|
@APP.route('/favicon.ico')
|
|
|
|
def favicon():
|
|
|
|
return send_from_directory(os.path.join(APP.root_path, 'static'), 'favicon.ico')
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
if not 'index.json' in os.listdir('.'):
|
|
|
|
tfidf.create_index()
|
|
|
|
APP.debug=True
|
|
|
|
APP.run()
|