from flask import session, Blueprint, render_template, send_file, request
from lexos.managers import session_manager
from lexos.helpers import constants as constants
from lexos.views.base_view import detect_active_docs
from lexos.models.tokenizer_model import TokenizerModel
from lexos.models.filemanager_model import FileManagerModel

# this is a flask blue print
# it helps us to manage groups of views
# see here for more detail:
# http://exploreflask.com/en/latest/blueprints.html
# http://flask.pocoo.org/docs/0.12/blueprints/
tokenizer_blueprint = Blueprint('tokenizer', __name__)


# Tells Flask to load this function when someone is at '/tokenizer'
@tokenizer_blueprint.route("/tokenizer", methods=["GET", "POST"])
def tokenizer():
    # Detect the number of active documents.
    num_active_docs = detect_active_docs()
    id_label_map = \
        FileManagerModel().load_file_manager().get_active_labels_with_id()

    if request.method == "GET":
        # Fill session with default options.
        session['analyoption'] = constants.DEFAULT_ANALYZE_OPTIONS
        session['tokenizerOption'] = constants.DEFAULT_TOKENIZER_OPTIONS

        return render_template('tokenizer.html',
                               labels=id_label_map,
                               numActiveDocs=num_active_docs)
    else:
        file_path = TokenizerModel().download_dtm()
        return send_file(file_path,
                         as_attachment=True,
                         attachment_filename="tokenizer_result.csv")


@tokenizer_blueprint.route("/tokenizeTable", methods=["POST"])
def tokenizer_result():
    # Cache the front options for matrix model and tokenizer model.
    session_manager.cache_analysis_option()
    session_manager.cache_tokenizer_option()
    # Return the generated DTM to ajax call.
    return TokenizerModel().get_dtm()


@tokenizer_blueprint.route("/tokenizerDownload", methods=["POST"])
def tokenizer_download():
    # Cache front end result and return rendered template for download.
    session_manager.cache_analysis_option()
    session_manager.cache_tokenizer_option()
    file_path = TokenizerModel().download_dtm()
    return send_file(file_path,
                     as_attachment=True,
                     attachment_filename="tokenizer_result.csv")

@@ -960,17 +960,46 @@
       # 
-F
+When first get to this page, f
 ill sess
@@ -1164,16 +1164,75 @@
 OPTIONS%0A
+        # Return rendered template wih desired information.
 %0A       
@@ -1409,51 +1409,339 @@
     
-file_path = TokenizerModel().download_dtm()
+# If post method is get, do the download.%0A        # First cache the useful options.%0A        session_manager.cache_analysis_option()%0A        session_manager.cache_tokenizer_option()%0A        # Generate file and get the file path.%0A        file_path = TokenizerModel().download_dtm()%0A        # Return the file by sending the file path.
 %0A   
@@ -2220,446 +2220,4 @@
 m()%0A
-%0A%0A@tokenizer_blueprint.route(%22/tokenizerDownload%22, methods=%5B%22POST%22%5D)%0Adef tokenizer_download():%0A    # Cache front end result and return rendered template for download.%0A    session_manager.cache_analysis_option()%0A    session_manager.cache_tokenizer_option()%0A    file_path = TokenizerModel().download_dtm()%0A    return send_file(file_path,%0A                     as_attachment=True,%0A                     attachment_filename=%22tokenizer_result.csv%22)%0A

Add more comments and remove useless code