<commit_before>from flask import Flask, abort, jsonify, request, render_template
import wikibard, wikiserver
from  werkzeug.debug import get_current_traceback
import yaml
import dotmatrix
from flask.ext.cors import CORS
import wikipedia

print_to_dot_matrix = True

# EB looks for an 'application' callable by default.
application = Flask(__name__)
cors = CORS(application, resources={r"/api/*": {"origins": "*"}})

@application.route('/', methods=['GET'])
def index():
    return render_template('index.html')

@application.route('/search', methods=['GET'])
def search():
    query = request.args.get('q')
    query = query.replace("_", " ")
    print("Searching!")
    print(query)
    results = wikipedia.search(query)
    print(results)
    updated_results = []
    for result in results:
        updated_results.append(result.replace(" ", "_"))
    return jsonify(list=updated_results)


@application.route('/compose', methods=['POST'])
def compose():
    title = request.form.get("query")
    page = wikipedia.page(title.replace("_", " "))
    if not page:
        return render_template('index.html')
    print("Composing poem for " + title)
    poem = wikiserver.poemForPageTitle(title)
    poem_lines = poem.split('\n')
    title = title.replace("_", " ")
    images = wikiserver.imagesForPageTitle(title)

    if print_to_dot_matrix:
        dotmatrix.printPoem(title, poem_lines)

    if images:
        image = images[0]
        return render_template('poem.html', title=title, poem_lines=poem_lines, image=image)
    else:
        return render_template('poem.html', title=title, poem_lines=poem_lines)<commit_msg>redirect to index if you enter an invalid page (i know this is not the best ux but it is a step up from crashing)<commit_after>from flask import Flask, abort, jsonify, request, render_template, redirect, url_for
import wikibard, wikiserver
from  werkzeug.debug import get_current_traceback
import yaml
import dotmatrix
from flask.ext.cors import CORS
import wikipedia

print_to_dot_matrix = True

# EB looks for an 'application' callable by default.
application = Flask(__name__)
cors = CORS(application, resources={r"/api/*": {"origins": "*"}})

@application.route('/', methods=['GET'])
def index():
    return render_template('index.html')

@application.route('/search', methods=['GET'])
def search():
    query = request.args.get('q')
    query = query.replace("_", " ")
    print("Searching!")
    print(query)
    results = wikipedia.search(query)
    print(results)
    updated_results = []
    for result in results:
        updated_results.append(result.replace(" ", "_"))
    return jsonify(list=updated_results)


@application.route('/compose', methods=['POST'])
def compose():
    title = request.form.get("query")
    try:
        page = wikipedia.page(title.replace("_", " "))
    except wikipedia.exceptions.DisambiguationError:
        return redirect(url_for('index'))

    if not page:
        return redirect(url_for('index'))
    print("Composing poem for " + title)
    poem = wikiserver.poemForPageTitle(title)
    poem_lines = poem.split('\n')
    title = title.replace("_", " ")
    images = wikiserver.imagesForPageTitle(title)

    if print_to_dot_matrix:
        dotmatrix.printPoem(title, poem_lines)

    if images:
        image = images[0]
        return render_template('poem.html', title=title, poem_lines=poem_lines, image=image)
    else:
        return render_template('poem.html', title=title, poem_lines=poem_lines)
{'fraction_non_alphanumeric': 0.1330842196033343, 'fraction_numerical': 0.0005748778384593274, 'mean_word_length': 4.163204747774481, 'pattern_counts': {'":': 4, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5792877', 'n_tokens_mistral': 1096, 'n_tokens_neox': 1073, 'n_words': 252}