def index():
    form = CreateInputForm(csrf_enabled=False)
    if form.validate_on_submit():
        book = form.book.data
        chapter = form.chapter.data
        verse = form.verse.data
        depth = form.depth.data
        selected_works = form.works.data.split("%")
        session['works'] = selected_works

        print 'Works:'
        print session['works']
        return redirect(url_for('.graph', book=book, chapter=chapter, verse=verse, depth=depth))

    #50 chapters in Gen, 31 vereses in Chp 1. range function is non-inclusive, so end of range is set to 51 and 32 respectively
    return render_template('index.html',
                            books=books,
                            chapters=range(1, len(books_dict['Genesis']) + 1),
                            verses=range(1, books_dict['Genesis'][0] + 1),
                            cur_book='Genesis',
                            form=form,
                            works=works,
                            selected_works=[],
                            depth=1)
def graph(book, chapter, verse, depth):
    #TODO: Validate book, chapter, and verse
    form = CreateInputForm(csrf_enabled=False)
    seed_verse = str(book) + " " + str(chapter) + ":" + str(verse)
    depth = int(depth)


    #print form.validate_on_submit();
    if form.validate_on_submit():
        new_book = form.book.data
        new_chapter = form.chapter.data
        new_verse = form.verse.data
        depth = form.depth.data
        selected_works = form.works.data.split("%")
        print selected_works
        session['works'] = selected_works
        #return redirect(url_for('.graph', book=new_book, chapter=new_chapter, verse=new_verse, depth=depth))


    nodes_dict = defaultdict(str)
    nodes_dict[seed_verse] = str(0)

    #depth = 2;

    nodes = [seed_verse]
    new_verses = [seed_verse]
    edges = []
    edge_labels = []
    link_num = 0
    selected_works = session['works']
    print selected_works
    for i in range(depth):
        new_new_verses = []
        for new_verse in new_verses:
            link_dict = grab_data(new_verse)

            for link in link_dict:
                sources = link_dict[link]

                if len(selected_works) > 0 and selected_works[0]:
                    regex = "^" + selected_works[0]
                    for selected_work in selected_works[1:]:
                        regex = regex + "|^" + selected_work
                    new_sources = []
                    for source in sources:
                        m = re.search(regex, source)
                        if m:
                            new_sources.append(source)

                    sources = new_sources
                if len(sources) > 0:
                    if not nodes_dict[str(link)]:
                        link_num = link_num + 1
                        nodes_dict[str(link)] = str(link_num)
                        nodes.append(str(link))
                        new_new_verses.append(str(link))
                    for source in sources:
                        edges.append(nodes_dict[new_verse] + "-" + nodes_dict[link])
                        edge_labels.append(str(source))
        new_verses = new_new_verses


    return render_template('graph.html', #TODO: Change to graph.html
                            books=books,
                            chapters=range(1, len(books_dict[book]) + 1),
                            verses=range(1, books_dict[book][int(chapter)-1] + 1),
                            form=form,
                            cur_book=str(book),
                            cur_chapter=int(chapter),
                            cur_verse=int(verse),
                            nodes=",".join(nodes),
                            edges=",".join(edges),
                            edge_labels="/".join(edge_labels),
                            seed_verse=seed_verse,
                            works=works,
                            selected_works=selected_works,
                            depth=depth)