Tuesday, July 27, 2021

Hello, Flask from Google Colab !

Flask is really simple framework. 

You can run in it google colab also using flask-ngrok. 

Following is a running example in google colab. You need index.html and Welcome.html in the /content folder. 

Also note that when you are referencing html files, the file names are case-sensitive! If casing does not match flask will not find the file. 

Secondly, you need to tell flask the folder of html file when declaring app=Flask(__name__ , template_foler="XXXX")


#####Flask File

# #!pip install flask
# #!pip install flask-ngrok

from flask import Flask
import flask_ngrok

from flask import Flask, redirect, url_for, request , render_template
app = Flask(__name__, template_folder = "/content")
flask_ngrok.run_with_ngrok(app)

@app.route("/")
def RedirectToMyForm() : 
  return redirect(url_for("myform" , name="ere"))

@app.route('/greet/<name>')
def greet(name):
   #return "Welcome %s" %name  #<= This also works for simple situations
   return render_template("Welcome.html" , name=name)
   
  
@app.route('/myform',methods = ['POST''GET'])
def myform():
   if request.method == 'POST':
      user = request.form['nm'] #this must match "name" control in form (name="nm")
      return redirect(url_for('greet',name = user))
   else:
     return render_template("index.html")
  
if __name__ == '__main__':
   app.run()



index.html

<html>
<head>
</head>
<body>
<form method="POST">
<input type="text" name="nm" placeholder="name"></input>
<button type="submit">Submit</button>
</form>
</body>
</html>



Welcome.html

<html>
<head>
</head>
<body>

<h1>Hi, {{name}} !</h1>
</body>
</html>









No comments:

Post a Comment

 using Microsoft.AspNetCore.Mvc; using System.Xml.Linq; using System.Xml.XPath; //<table class="common-table medium js-table js-stre...