Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
suyashi29
GitHub Repository: suyashi29/python-su
Path: blob/master/Python core/app.py.ipynb
3074 views
Kernel: Python 3
from flask import Flask app = Flask(__name__) @app.route('/') def hello_world(): return 'Python Session' if __name__ == '__main__': app.run()
  • name is a variable that Python automatically creates, and it’s equal to "main" when it’s you that ran the code (as opposed to another script running the code). You’ll see this line a lot while doing Python, so it’s good to get used to it.

from flask import Flask app = Flask(__name__) @app.route('/') def hello_world(): # Link to test page return 'To see the test route go to /test' @app.route('/test') def testing(): return 'You got to the testing route!' if __name__ == '__main__': app.run(host='0.0.0.0') #http://localhost:5000/
from flask import Flask app = Flask(__name__) @app.route('/') def hello_world(): return 'Open the URL in another tab, and then go to /hello/yourname' @app.route('/hello/<name>') def hello_name(name): return 'Hello '+ name + '!' if __name__ == '__main__': app.run(host='0.0.0.0')
from flask import Flask app = Flask(__name__) def factors(num): return [x for x in range(1, num+1) if num%x==0] @app.route('/') def home(): return "Open the URL and go to /factors/num to see the website" @app.route('/factors/<int:num>') def factors_route(num): return "The factors of {} are {}".format(num, factors(num)) if __name__ == '__main__': app.run(host='0.0.0.0')
from flask import Flask app = Flask(__name__) def factors(num): return [x for x in range(1, num+1) if num%x==0] @app.route('/') def home(): return '<a href="/factors_raw/100"> click here for an example</a>' @app.route('/factors_raw/<int:n>') def factors_display_raw_html(n): factors_list = factors(int(n)) # First we put the stuff at the top, adding "n" in there html = "<h1> The factors of "+str(n)+" are</h1>"+"\n"+"<ul>" # for each factor, we make a <li> item for it for f in factors_list: html += "<li>"+str(f)+"</li>"+"\n" html += "</ul> </body>" # the close tags at the bottom return html if __name__ == '__main__': app.run(host='0.0.0.0')