0

I am relatively new to python and have setup a local virtualenviorenment on my mac desktop. I am utilizing the Flask, jsonify, and requests frameworks.

Inside of myvirtualenviorenment I have two files: one that just uses flask and one which uses flask, jsonify, and requests frameworks. I believe I have imported all these frameworks correctly, but when I run the second file, which is a GET request, using all 3 frameworks, I dont get a response at all from terminal.

However, I am able to access 127.0.0.1:5000 when just using the Flask framework. Did I not properly import the jsonify and request frameworks?

Below is my code for the non-working file:

from flask import Flask, jsonify, request #import objects from the Flask model
app = Flask(__name__) #define app using Flask

@app.route('/', methods=['GET'])
def test():
    return jsonify({'message' : 'It works!'})

    if __name__== '__main__':
        app.run(debug=True, port=8080) #run app on port 8080 in debug mode
9
  • Which file did you run? this exact one? Commented Aug 3, 2017 at 21:26
  • correct. this is the non-working file that I have tried running. does it have something to do with the port being 8080? Commented Aug 3, 2017 at 21:26
  • 3
    Try to access you server on 127.0.0.1:8080 Commented Aug 3, 2017 at 21:28
  • well i am trying to run: python restful.py (my file name, and what is shown above), but nothing happens. how can i change my sever to port 8080 when running python? Commented Aug 3, 2017 at 21:36
  • You have set the port to be 8080, so what you want to do now is just running this file and accessing 127.0.0.1:8080 Commented Aug 3, 2017 at 21:40

2 Answers 2

3

Please go ahead and unindent

if __name__== '__main__':
        app.run(debug=True, port=8080) #run app on port 8080 in debug mode

Running this file with python will now start the development server.

Sign up to request clarification or add additional context in comments.

2 Comments

it was an indentation issue after all...i feel silly. thank you for catching this
You're welcome. Later, when you build bigger projects, you may have a file run.py that contains only the app.run part so you keep your project modularized.
1

Your indentation seems wrong def test() should not be indented, is this the way the code is written or just the way you copied? The same goes to if __name__ == '__main__'

3 Comments

This shoud be just a comment, not an answer!
i actually messed it up indenting it in stackoverflow. i have corrected it above and it now reflects what my code actually looks like with indentations.
That could have been the reason the code is not working

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.