pythonserver-side

Process to Upload a run a Python Hello World script via IDE and accessed through browser


I have googled this and cannot find an answer. I am coming from PHP. In PHP I would write my code in my IDE on my laptop, upload the .php script to my server (VPS), and then execute/run the script through web browser.

I am new to Python. I have written the following code

print("Hello World") 

in a file called hello.py.

If I upload the file to my server and access it through google chrome, I get the following:

print("Hello, World!")

Python is installed and the version is Python 3.x In the PHP world I would conclude the server isnt parsing the PHP code. The hello.py file is not in the /bin/ folder on the server which I understand is fine with my Python install.

Any advice please on how I can get the file to run thru browser? Thanks


Solution

  • Python is different than PHP. PHP is a server side scripting language. Python is a high level interpreted language. Python can be easily interacted with from the terminal itself. But if you still want your output to be shown on a browser window I would suggest you check out Flask. Here is your simple code to print hello world in the browser:

    from flask import Flask
    
    app = Flask(__name__)
    
    @app.route("/")
    def hello_world():
        # Do anything in this function and then return the final value to be displayed on the browser
        return "<h1>Hello world</h1>"
    
    if __name__ == '__main__':
        app.run(host='localhost', port=5000, debug=True)
    

    This is a simple web app which is running on local host (http://localhost:5000/). If it does not run on port 5000 try changing it to some other port.