import numpy into flask app

Question:

I’m was trying to run my python code on a web server and found out i have to use flask or django so I decided to use flask and after reading some articles i wrote a hello world script and it ran but when i tried do more complex things like import numpy and pandas I kept giving me internal server errors and I have googled it but haven’t got what I wanted. Please is this possible with flask or is there a better way to run my script online and am still a new to this. Thank you in advance. This is the script am trying to run

import numpy as np
import pandas as pd
from flask import Flask
app = Flask(__name__)
@app.route("/")
def hello():
  file = pd.read_csv('movies.csv')

  print('man')

if __name__ == "__main__":
  app.run()

This is the logs

 * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
 man
 [2017-03-12 22:12:06,757] ERROR in app: Exception on / [GET]
 Traceback (most recent call last):
  File "C:UsershpAppDataLocalProgramsPythonPython35libsite-packagesflaskapp.py", line 1982, in wsgi_app
  response = self.full_dispatch_request()
File "C:UsershpAppDataLocalProgramsPythonPython35libsite-packagesflaskapp.py", line 1615, in full_dispatch_
 request
return self.finalize_request(rv)
File "C:UsershpAppDataLocalProgramsPythonPython35libsite-packagesflaskapp.py", line 1630, in finalize_request
response = self.make_response(rv)
File "C:UsershpAppDataLocalProgramsPythonPython35libsite-packagesflaskapp.py", line 1725, in make_response
raise ValueError('View function did not return a response')
 ValueError: View function did not return a response
 127.0.0.1 - - [12/Mar/2017 22:12:06] "GET / HTTP/1.1" 500
Asked By: slayer

||

Answers:

The error is not ought due to the numpy import but due to the fact that your hello view does not return anything (as you can see from the traceback).

ValueError: View function did not return a response

The view just stores movies.csv in a variable. Nothing more. The client awaits for a response which never gets.

Try, after the print('man') statement to write something like this:

return 'It worked'

Remember: each view function handles a request and must always return a response (whether its a 404 error, a text, an HTML page or something else).

Answered By: nik_m
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.