Forums

python everywhere problem when using 2 html pages

Hi, i have a problem when i'm using python everywhere, i have my scrpit "main.py", and 2 html pages, it works very well when i do on local host but when i'm using python everywhere only the first html page work and for the second i have an error message: "Internal Server Error The server encountered an internal error and was unable to complete your request. Either the server is overloaded or there is an error in the application."

here is my main.py:

!/usr/bin/env python3

-- coding: utf-8 --

""" Created on Wed Aug 14 12:08:23 2019

@author: DG """

from flask import request, redirect, render_template, Flask, url_for import pandas as pd import tempfile

import re from nltk.util import ngrams from nltk import everygrams from collections import Counter from nltk.corpus import stopwords

def count_words(s, n_gram_min, n_gram_max, nb_words): stop_words = set(stopwords.words("english")) l_w = ['aren',"aren't","couldn'",'couldn',"couldn't",'didn',"didn't",'doesn',"doesn't",'don',"don't","hadn't",'hasn', "hasn't",'isn',"isn't",'mightn',"mightn't",'mustn',"mustn't",'needn',"needn't",'no','nor','not',"shan't",'shouldn', "shouldn't",'wasn',"wasn't","won't",'wouldn',"wouldn't"] for i in l_w: stop_words.discard(i) s = s.lower() s = re.sub(r'[^a-zA-Z0-9\s]', ' ', s) tokens = [token for token in s.split(" ") if token != ""] filtered_sentence = [] for w in tokens: if w not in stop_words: filtered_sentence.append(w) output = list(everygrams(filtered_sentence, n_gram_min, n_gram_max)) c = Counter(output) counts = c.most_common(nb_words) return counts

app = Flask(name)

@app.route('/') def home(): return render_template("home.html")

@app.route("/result", methods = ["GET", "POST"]) def result(): if request.files: data = request.files["file"]

tempfile_path = tempfile.NamedTemporaryFile().name
data.save(tempfile_path)
df = pd.read_excel(tempfile_path)

n_word = request.form.get('n_word', type=int)
n_raw = request.form.get('n_raw', type=int)
data = df.iloc[:,[3, 6]]
for i in range(0, data.shape[0]):
    if data.at[i,'Review Rating'] > 3:
        data.at[i,'Score'] = 1
    elif data.at[i,'Review Rating'] < 4:
        data.at[i,'Score'] = 0
data = data.drop(['Review Rating'], axis = 1)
data_bad = data.loc[data['Score'] == 0]
data_good = data.loc[data['Score'] == 1]
bad_reviews = ''
for x in data_bad['Review Text']:
    bad_reviews = bad_reviews + str(x)
good_reviews = ''
for i in data_good['Review Text']:
    good_reviews = good_reviews + str(i)
count_bad_words_reviews = count_words(bad_reviews, n_word, n_word, n_raw)
count_good_words_reviews = count_words(good_reviews, n_word, n_word, n_raw)


return render_template("result.html", result1 = count_bad_words_reviews, result2 = count_good_words_reviews)

if name == 'main': app.run(debug=True)

and here is my 2 html: home.html: <!DOCTYPE html> <html> <head> <title>Home</title> <!-- <link rel="stylesheet" type="text/css" href="../static/css/styles.css"> --> <link rel="stylesheet" type="text/css" href="{{ url_for('static', filename='css/styles.css') }}"> </head> <body>

<header>
    <div class="container">

    <h2>Most common word in reviews</h2>

</div>
</header>

<div class="ml-container">

    <form action="{{ url_for('result')}}" method="POST" enctype="multipart/form-data">
    <p>Upload file</p>
    <!-- <input type="text" name="comment"/> -->
    <input type="file" name="file" id="file">
    <input type="submit" value="Upload file" name="submit">

    <label for="n_word">Number of words</label>
        <select id="n_word" name="n_word">                
            <option type="number" value="2">2</option>
            <option type="number" value="3">3</option>
            <option type="number" value="4">4</option>
        </select>
    <label for="n_raw">Number of raws</label>
        <select id="n_raw" name="n_raw">                
            <option type="number" value="10">10</option>
            <option type="number" value="20">20</option>
            <option type="number" value="30">30</option>
            <option type="number" value="40">40</option>
            <option type="number" value="50">50</option>
        </select>

    <input type="submit" class="btn-info" value="predict">

</form>

</div>

</body> </html>

and result.html: <!doctype html> <html> <body> <h1> List of bad word: {{ result1 }}</h1> <h1> List of good word: {{ result2 }}</h1> </body> </html>

of cours my html pages are in the templates folder.

thank you

don't use app.run()

Look in your error and server logs for tracebacks that you can use to start debugging the problem.

i change some things:

from flask import request, redirect, render_template, Flask import pandas as pd import tempfile import re import nltk nltk.download() from nltk.util import ngrams from nltk import everygrams from collections import Counter from nltk.corpus import stopwords

def count_words(s, n_gram_min, n_gram_max, nb_words): stop_words = set(stopwords.words("english")) l_w = ['aren',"aren't","couldn'",'couldn',"couldn't",'didn',"didn't",'doesn',"doesn't",'don',"don't","hadn't",'hasn', "hasn't",'isn',"isn't",'mightn',"mightn't",'mustn',"mustn't",'needn',"needn't",'no','nor','not',"shan't",'shouldn', "shouldn't",'wasn',"wasn't","won't",'wouldn',"wouldn't"] for i in l_w: stop_words.discard(i) s = s.lower() s = re.sub(r'[^a-zA-Z0-9\s]', ' ', s) tokens = [token for token in s.split(" ") if token != ""] filtered_sentence = [] for w in tokens: if w not in stop_words: filtered_sentence.append(w) output = list(everygrams(filtered_sentence, n_gram_min, n_gram_max)) c = Counter(output) counts = c.most_common(nb_words) return counts

app = Flask(name)

@app.route('/') def home(): return render_template("home.html")

@app.route("/result", methods = ["GET", "POST"]) def result(): if request.files: data = request.files["file"]

tempfile_path = tempfile.NamedTemporaryFile().name
data.save(tempfile_path)
df = pd.read_excel(tempfile_path)

n_word = request.form.get('n_word', type=int)
n_raw = request.form.get('n_raw', type=int)
data = df.iloc[:,[3, 6]]
for i in range(0, data.shape[0]):
    if data.at[i,'Review Rating'] > 3:
        data.at[i,'Score'] = 1
    elif data.at[i,'Review Rating'] < 4:
        data.at[i,'Score'] = 0
data = data.drop(['Review Rating'], axis = 1)
data_bad = data.loc[data['Score'] == 0]
data_good = data.loc[data['Score'] == 1]
bad_reviews = ''
for x in data_bad['Review Text']:
    bad_reviews = bad_reviews + str(x)
good_reviews = ''
for i in data_good['Review Text']:
    good_reviews = good_reviews + str(i)
count_bad_words_reviews = count_words(bad_reviews, n_word, n_word, n_raw)
count_good_words_reviews = count_words(good_reviews, n_word, n_word, n_raw)


return render_template("result.html", result1 = count_bad_words_reviews, result2 = count_good_words_reviews)

if name == 'main': app.run(debug=True)

and this the error tracebacks:

2019-08-19 17:34:48,125: Error running WSGI application 2019-08-19 17:34:48,126: EOFError: EOF when reading a line 2019-08-19 17:34:48,126: File "/var/www/dgolcheh_pythonanywhere_com_wsgi.py", line 16, in <module> 2019-08-19 17:34:48,126: from main import app as application # noqa 2019-08-19 17:34:48,126: 2019-08-19 17:34:48,127: File "/home/dgolcheh/mysite/main.py", line 7, in <module> 2019-08-19 17:34:48,127: nltk.download() 2019-08-19 17:34:48,127: 2019-08-19 17:34:48,127: File "/home/dgolcheh/.virtualenvs/reviewapp/lib/python3.7/site-packages/nltk/downloader.py", line 773, in download 2019-08-19 17:34:48,128: self._interactive_download() 2019-08-19 17:34:48,128: 2019-08-19 17:34:48,128: File "/home/dgolcheh/.virtualenvs/reviewapp/lib/python3.7/site-packages/nltk/downloader.py", line 1125, in _interactive_download 2019-08-19 17:34:48,128: DownloaderShell(self).run() 2019-08-19 17:34:48,129: 2019-08-19 17:34:48,129: File "/home/dgolcheh/.virtualenvs/reviewapp/lib/python3.7/site-packages/nltk/downloader.py", line 1154, in run 2019-08-19 17:34:48,129: user_input = input('Downloader> ').strip() 2019-08-19 17:34:48,129: ******* 2019-08-19 17:34:48,129: If you're seeing an import error and don't know why, 2019-08-19 17:34:48,130: we have a dedicated help page to help you debug: 2019-08-19 17:34:48,130: https://help.pythonanywhere.com/pages/DebuggingImportError/ 2019-08-19 17:34:48,130: *******

if you can help me to understand what is the problem thank you

You can't use the nltk downloader in a web app, because it tries to get input from the console and there is not one available in a web app. Download what you need first in a console and then just use it in the web app.