0

I am really new to this python website thing. I have a VaderSentiment code that returns if a string input was positive, negative, or neutral. The code looks like this :

from vaderSentiment.vaderSentiment import SentimentIntensityAnalyzer


# function to print sentiments
# of the sentence.
def sentiment_scores(sentence):
 
    # Create a SentimentIntensityAnalyzer object.
    sid_obj = SentimentIntensityAnalyzer()
 
    # polarity_scores method of SentimentIntensityAnalyzer
    # object gives a sentiment dictionary.
    # which contains pos, neg, neu, and compound scores.
    sentiment_dict = sid_obj.polarity_scores(sentence)
     
    print("Overall sentiment dictionary is : ", sentiment_dict)
    print("sentence was rated as ", sentiment_dict['neg']*100, "% Negative")
    print("sentence was rated as ", sentiment_dict['neu']*100, "% Neutral")
    print("sentence was rated as ", sentiment_dict['pos']*100, "% Positive")
 
    print("Sentence Overall Rated As", end = " ")
 
    # decide sentiment as positive, negative and neutral
    if sentiment_dict['compound'] >= 0.05 :
        print("Positive")
 
    elif sentiment_dict['compound'] <= - 0.05 :
        print("Negative")
 
    else :
        print("Neutral")
 
 
   
# Driver code
if __name__ == "__main__" :
 
    print("\n1st statement :")
    sentence = input("Please enter a string:\n")
    # function calling
    sentiment_scores(sentence)

    sentence = input("Please enter a second string:\n")
    # function calling
    sentiment_scores(sentence)

    sentence = input("Please enter a third string:\n")
    # function calling
    sentiment_scores(sentence)

def fonction():
    return 0

I also have another page that runs this script :

from flask import Flask, render_template
from vaderSentiment.vaderSentiment import SentimentIntensityAnalyzer
import main

app = Flask(__name__)

@app.route("/",methods=['GET'])
def code():
    out = open(r'main.py', 'r').read()
    return exec(out)

if __name__ == '__main__':
  app.run(debug=True)

When I execute the code (flask), it starts a webpage that keeps loading, and the input is displayed in my IDE instead of the webpage.

I wish to print on the webpage inputs as well as results.

Any help would be greatly appreciated :) Thanks in advance for your help.

  • Your indentation is clearly wrong, but what’s less clear is how this ought to be indented. Don’t make us guess; please [edit] to fix this. (On the desktop version of this site, paste your program, select it, and type ctrl-K to properly format it as code.) – tripleee Aug 14 '22 at 14:02
  • The fundamental problem is that your function should `return` the result rather than `print` it. The caller can then decide what to do with it, and thus e.g. generate a web page with the response instead. – tripleee Aug 14 '22 at 14:04
  • I've changed indentation as well as return type. Still not working as intended... – CenturyGhost Aug 14 '22 at 17:47

1 Answers1

-1

You should make index.html or any name HTML page and return this page with render_template like this

app.route('/')
def something():
    return render_template('index.html')

and important thing you must put the index.html or what ever name page in folder called templates to work correctly.

  • Thanks a lot. I could organize my code accordingly and indeed, make something out of it, unlike the other comment. Kuddos ! :) – CenturyGhost Aug 15 '22 at 11:53