0

I am writing a code for Aspect Based Sentiment analysis in python and also developing a web application using flask. I created a bar graph for sentiment analysis and save this graph on specific folder and now i want to display this graph on flask application. when i am displaying this graph into html web page, it shows only the icon of image not actual image. Here is my python code to create bar graph using my data frame:

d = {'Aspect': Aspect, 'Positive': Positive, 'Neutral': Neutral, 'Negative': Negative}
df = pd.DataFrame(d)
print(df)
df.plot.bar(x="Aspect", y=["Negative", "Neutral", "Positive"], title = "Sentiment Analysis");

plt.savefig(os.path.join('E:/Projects/review/static', 'bar.png'))
full_filename = os.path.join('E:/Projects/review/static/', 'bar.png')
return render_template('view.html',user_image =full_filename)

Here is my python code of view.html file:

 <h2>Sentiment</h2>
      <h1><img src="{{ user_image }}" alt="User Image"></img></h1>
Sandeep
  • 369
  • 1
  • 5
  • 16

1 Answers1

0

You need to serve the static file with nginx or just use flask's send_from_directory

from flask import Flask, request, send_from_directory

# set the project root directory as the static folder, you can set others.
app = Flask(__name__, static_url_path='')

@app.route('/img/<path:path>')
def send_img(path):
    return send_from_directory('static', path)

And as user_image pass just bar.png

Note that this is not suitable for production and stuff. The real solution should be with you setting up nginx or any other web server to serve static files.

You can find more info about it there: http://flask.pocoo.org/docs/0.12/quickstart/#static-files

Olaf Górski
  • 166
  • 5