<gh_stars>0
import json
import plotly
import pandas as pd

from nltk.stem import WordNetLemmatizer
from nltk.tokenize import word_tokenize

from flask import Flask
from flask import render_template, request, jsonify
from plotly.graph_objs import Bar
from sklearn.externals import joblib
from sqlalchemy import create_engine


app = Flask(__name__)

def tokenize(text):
    tokens = word_tokenize(text)
    lemmatizer = WordNetLemmatizer()

    clean_tokens = []
    for tok in tokens:
        clean_tok = lemmatizer.lemmatize(tok).lower().strip()
        clean_tokens.append(clean_tok)

    return clean_tokens

# load data
engine = create_engine('sqlite:///../data/DisasterDB.db')
df = pd.read_sql_table('disaster_messages', engine)

# load model
model = joblib.load("../models/classifier.pkl")


# index webpage displays cool visuals and receives user input text for model
@app.route('/')
@app.route('/index')
def index():
    
    # extract data needed for visuals
    # TODO: Below is an example - modify to extract data for your own visuals

    # Create values for Y values in bar by grouping the messages based on genre
    genre_counts = df.groupby('genre').count()['message']
    # Create the names for X values in bar of grouping the messages based on genre
    genre_names = list(genre_counts.index)


    # Use categories data only without other features
    categories = df.drop(['original', 'genre','message','id'], axis =1)
    # Sum the values for each category and save the for Y values of the bar
    categories_counts = categories.sum().sort_values(ascending=False)
    # Create for the names of the categories and save them for X values bar
    categories_names = list(categories_counts.index)
    

    # Find the messages are under genre Social only
    social = df[df['genre'] == 'social']
    # Drop othere unneeded features
    social_droped = social.drop(['original', 'genre','message','id'], axis =1)
    # Sum the categories values for genre Social
    social_counts = social_droped.sum().sort_values(ascending=False)
    # Create for the names of the categories and save them for X values bar
    social_names = list(social_counts.index)
    
    # create visuals
    # TODO: Below is an example - modify to create your own visuals
    graphs = [
        {
            'data': [
                Bar(
                    x=genre_names,
                    y=genre_counts
                )
            ],

            'layout': {
                'title': 'Distribution of Message Genres',
                'yaxis': {
                    'title': "Count"
                },
                'xaxis': {
                    'title': "Genre"
                }
            }
        },
        {
            'data': [
                Bar(
                    x=categories_names,
                    y=categories_counts
                )
            ],

            'layout': {
                'title': 'Distribution of Messages Categories',
                'yaxis': {
                    'title': "Count"
                },
                'xaxis': {
                    'title': "Categories"
                }
            }
        },
        {
            'data': [
                Bar(
                    x=social_names,
                    y=social_counts
                )
            ],

            'layout': {
                'title': 'Distribution of Social Messages Categories',
                'yaxis': {
                    'title': "Count"
                },
                'xaxis': {
                    'title': "Categories"
                }
            }
        }
    ]
    
    # encode plotly graphs in JSON
    ids = ["graph-{}".format(i) for i, _ in enumerate(graphs)]
    graphJSON = json.dumps(graphs, cls=plotly.utils.PlotlyJSONEncoder)
    
    # render web page with plotly graphs
    return render_template('master.html', ids=ids, graphJSON=graphJSON)


# web page that handles user query and displays model results
@app.route('/go')
def go():
    # save user input in query
    query = request.args.get('query', '') 

    # use model to predict classification for query
    classification_labels = model.predict([query])[0]
    classification_results = dict(zip(df.columns[4:], classification_labels))

    # This will render the go.html Please see that file. 
    return render_template(
        'go.html',
        query=query,
        classification_result=classification_results
    )


def main():
    app.run(host='0.0.0.0', port=3001, debug=True)


if __name__ == '__main__':
    main()
import json
import plotly
import pandas as pd

from nltk.stem import WordNetLemmatizer
from nltk.tokenize import word_tokenize

from flask import Flask
from flask import render_template, request, jsonify
from plotly.graph_objs import Bar
from sklearn.externals import joblib
from sqlalchemy import create_engine


app = Flask(__name__)

def tokenize(text):
    tokens = word_tokenize(text)
    lemmatizer = WordNetLemmatizer()

    clean_tokens = []
    for tok in tokens:
        clean_tok = lemmatizer.lemmatize(tok).lower().strip()
        clean_tokens.append(clean_tok)

    return clean_tokens

# load data
engine = create_engine('sqlite:///../data/DisasterDB.db')
df = pd.read_sql_table('disaster_messages', engine)

# load model
model = joblib.load("../models/classifier.pkl")


# index webpage displays cool visuals and receives user input text for model
@app.route('/')
@app.route('/index')
def index():
    
    # extract data needed for visuals
    # TODO: Below is an example - modify to extract data for your own visuals

    # Create values for Y values in bar by grouping the messages based on genre
    genre_counts = df.groupby('genre').count()['message']
    # Create the names for X values in bar of grouping the messages based on genre
    genre_names = list(genre_counts.index)


    # Use categories data only without other features
    categories = df.drop(['original', 'genre','message','id'], axis =1)
    # Sum the values for each category and save the for Y values of the bar
    categories_counts = categories.sum().sort_values(ascending=False)
    # Create for the names of the categories and save them for X values bar
    categories_names = list(categories_counts.index)
    

    # Find the messages are under genre Social only
    social = df[df['genre'] == 'social']
    # Drop othere unneeded features
    social_droped = social.drop(['original', 'genre','message','id'], axis =1)
    # Sum the categories values for genre Social
    social_counts = social_droped.sum().sort_values(ascending=False)
    # Create for the names of the categories and save them for X values bar
    social_names = list(social_counts.index)
    
    # create visuals
    # TODO: Below is an example - modify to create your own visuals
    graphs = [
        {
            'data': [
                Bar(
                    x=genre_names,
                    y=genre_counts
                )
            ],

            'layout': {
                'title': 'Distribution of Message Genres',
                'yaxis': {
                    'title': "Count"
                },
                'xaxis': {
                    'title': "Genre"
                }
            }
        },
        {
            'data': [
                Bar(
                    x=categories_names,
                    y=categories_counts
                )
            ],

            'layout': {
                'title': 'Distribution of Messages Categories',
                'yaxis': {
                    'title': "Count"
                },
                'xaxis': {
                    'title': "Categories"
                }
            }
        },
        {
            'data': [
                Bar(
                    x=social_names,
                    y=social_counts
                )
            ],

            'layout': {
                'title': 'Distribution of Social Messages Categories',
                'yaxis': {
                    'title': "Count"
                },
                'xaxis': {
                    'title': "Categories"
                }
            }
        }
    ]
    
    # encode plotly graphs in JSON
    ids = ["graph-{}".format(i) for i, _ in enumerate(graphs)]
    graphJSON = json.dumps(graphs, cls=plotly.utils.PlotlyJSONEncoder)
    
    # render web page with plotly graphs
    return render_template('master.html', ids=ids, graphJSON=graphJSON)


# web page that handles user query and displays model results
@app.route('/go')
def go():
    # save user input in query
    query = request.args.get('query', '') 

    # use model to predict classification for query
    classification_labels = model.predict([query])[0]
    classification_results = dict(zip(df.columns[4:], classification_labels))

    # This will render the go.html Please see that file. 
    return render_template(
        'go.html',
        query=query,
        classification_result=classification_results
    )


def main():
    app.run(host='0.0.0.0', port=3001, debug=True)


if __name__ == '__main__':
    main()
# load data # load model # index webpage displays cool visuals and receives user input text for model # extract data needed for visuals # TODO: Below is an example - modify to extract data for your own visuals # Create values for Y values in bar by grouping the messages based on genre # Create the names for X values in bar of grouping the messages based on genre # Use categories data only without other features # Sum the values for each category and save the for Y values of the bar # Create for the names of the categories and save them for X values bar # Find the messages are under genre Social only # Drop othere unneeded features # Sum the categories values for genre Social # Create for the names of the categories and save them for X values bar # create visuals # TODO: Below is an example - modify to create your own visuals # encode plotly graphs in JSON # render web page with plotly graphs # web page that handles user query and displays model results # save user input in query # use model to predict classification for query # This will render the go.html Please see that file.