cross-reader/start.py

114 lines
3.5 KiB
Python
Raw Normal View History

#!/usr/bin/env python3
import os, sys
from sys import stdout
import flask
2019-03-07 23:37:37 +01:00
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'])
2019-07-10 21:19:51 +02:00
def index_():
"""
2019-07-10 21:19:51 +02:00
Displays the index page accessible at ''
Which is either the start page (index.html)
or a results page (results.html).
"""
query = None
results = None
query = request.args.get('q', '')
# Check printer argument
if printer.connected == True:
connection = 'connected'
else:
connection = 'connected'
2019-03-13 15:45:58 +01:00
if request.args.get('q', ''):
results, filenames, analytics = readings.request_results(query)
# print commands
if request.args.get('print', '') == 'now':
if connection == 'connected':
2019-03-14 17:22:33 +01:00
printer.printNow(query, results)
2019-07-10 21:19:51 +02:00
return flask.render_template('results.html', query=query, results=results, filenames=filenames, connection=connection, analytics=analytics)
else:
index = readings.load_index()
filenames = [manifesto for manifesto, _ in index.items()]
2019-03-07 23:37:37 +01:00
mappings_top = open('tfidf.top50.txt', 'r').readlines()
2019-07-10 21:19:51 +02:00
return flask.render_template('index.html', filenames=filenames, mappings=mappings_top)
2019-03-07 23:37:37 +01:00
2019-07-10 21:19:51 +02:00
@APP.route('/cross-readings', methods=['GET', 'POST'])
def crossreadings():
2019-03-07 23:37:37 +01:00
"""
2019-07-10 21:19:51 +02:00
Displays the cross-readings page accessible at 'cross-readings'.
2019-03-07 23:37:37 +01:00
"""
2019-07-10 21:19:51 +02:00
return flask.render_template('cross-readings.html')
2019-07-10 21:19:51 +02:00
@APP.route('/colophon', methods=['GET', 'POST'])
def colophon():
"""
2019-07-10 21:19:51 +02:00
Displays the colophon page accessible at 'colophon'.
"""
2019-07-10 21:19:51 +02:00
return flask.render_template('colophon.html')
2019-07-10 21:19:51 +02:00
@APP.route('/manifesto/<name>', methods=['GET', 'POST'])
def manifesto(name):
"""
2019-07-10 21:19:51 +02:00
Displays the page accessible at 'manifesto/<name>'.
It shows the original text of the manifesto in plain text.
"""
index = readings.load_index()
filenames = sorted([manifesto for manifesto, _ in index.items()])
manifesto = open('txt/'+name+'.txt', 'r').readlines()
link = manifestos[name]
2019-07-10 21:19:51 +02:00
return flask.render_template('manifesto.html', filenames=filenames, name=name, manifesto=manifesto, link=link)
2019-07-10 21:19:51 +02:00
@APP.route('/mapping/<name>', methods=['GET', 'POST'])
def contrast_mappings_name(name):
"""
2019-07-10 21:19:51 +02:00
Displays the page accessible at '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)
2019-07-10 21:19:51 +02:00
return flask.render_template('mapping.html', filenames=filenames, mappings=mappings[name], manifesto=name)
2019-07-10 21:19:51 +02:00
@APP.route('/list/<list_type>/<filename>', methods=['GET', 'POST'])
def render_list_for_document(list_type, filename):
"""
Show list of (TF / IDF / TF-IDF) values of one document.
"""
index = readings.load_index()
filenames = sorted([document for document, _ in index.items()])
values_list = [(value, word) for word, value in index[filename][list_type].items()]
2019-07-10 21:19:51 +02:00
return flask.render_template('list.html', filenames=filenames, list=values_list, list_type=list_type, filename=filename)
2019-03-07 23:37:37 +01:00
@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()
2019-07-10 21:20:55 +02:00
# APP.debug=True
APP.run()