Can't preview in aws c9

preview
workspace
python

#1

Hi, I made a new c9 account with AWS. I tried to run a simple application. It works with my pre-AWS c9 account, but not with an AWS c9 account. How can I get this to work?

Installed flask in a virtual environment first. The commands are:

$virtualenv venv
$. venv/bin/activate
$pip install flask 
$python3 helloworld.py

Here is the helloworld.py

#helloworld.py
#helloworld.py - the simplest flask app to run on c9
from flask import Flask
import os
app = Flask(name)

@app.route("/")
def hello():
    return "Hello World!"

if __name__ == '__main__':
    app.debug = True
    port = int(os.environ.get("PORT", 5000))
    app.run(host='0.0.0.0', port=port)

#2

If you’re using AWS Cloud9’s preview URL, you’ll need to run your code on 127.0.0.1 rather than 0.0.0.0


#3

Hi Bryceito,
Can you be more specific? I’ve tried a few different ways of adding the port/IP


#VERSION 1    
# if __name__ == '__main__':
#     app.run(host=os.getenv('IP', '0.0.0.0'), port=int(os.getenv('PORT', 8080)))
#     app.debug(True)

#VERSION 2
# if __name__ == '__main__':
#     app.debug = True
#     app.run(host=os.environ['IP'], port=os.environ['PORT']) 
    
#os.environ['IP'] is  127.0.0.1
#os.environ['PORT'] is  8080


    

#VERSION 3
if __name__ == '__main__':
    app.debug = True
    host = 'localhost' # '0.0.0.0' #"127.0.0.1" # I tried all of these ips
    port = int(os.environ.get("PORT", 8080)) 
    app.run(host=host, port=port)

For Version 1, 2 and 3, I get directed to http://127.0.0.1:8080/ which doesn’t load: ERR_CONNECTION_REFUSED

For Version 3
host = ‘localhost’ at least points me to a url that might be trying to load: http://34.237.143.162:8080/, but then fails with ERR_CONNECTION_TIMED_OUT

I am running these with the Green Run GUI button, not in the c9 terminal.

Thanks


#4

NVM, I figured it out - I wasn’t running/previewing the app correctly.

Running the App

To run the app, go to the terminal and type
python [NAMEOFFILE]
so
python app.py

Previewing the App

To view the app, go to Preview in C9 toolbar and click Preview Running App. You’ll be directed to a crazy url. Mine was https://a823459f29b04402a3793bec16fbXXXX.vfs.cloud9.us-east-1.amazonaws.com/ (edited a little)

Code in app.py

import os
from flask import Flask

app = Flask(__name__)

@app.route('/')
def hello():
    return "Let's double check"

if __name__ == '__main__':
    app.debug = True
    app.run(host=os.environ['IP'], port=os.environ['PORT'])

#5

There are two different URLs for workspaces: the private URL (the one that is currently working for you) and the public one (the IP address from the Share panel, which corresponds directly to your machine). You can use the public IP address as your Preview URL, but you will need to open up access to your host. You can find some documentation on how to do that here: https://docs.aws.amazon.com/cloud9/latest/user-guide/app-preview.html#app-preview-share

Hope this helps!