jeudi 20 juillet 2017

How to send data from flask to html template

I have a small application where input is taken from the user and based on it data is displayed back on the html. I have to send data from flask to display on html but unable to find a way to do it. There's no error that I encountered.

Python script :

from flask import Flask
from flask import render_template
from flask import request
import random

app = Flask(__name__, template_folder='templates')

@app.route('/', methods=['GET','POST'])
def samplefunction():
    return render_template('new.html')

    greetIn = ['hey', 'hi', 'hey there', 'hi there', 'hello', 'hola', 'yoo']
    byeIn = ['bye', 'see you later', 'catch you later', 'toodles']

    nameOut = ['my name is Fatty!!', 'Fatty is my name', 'you can call me Fatty', 'I go by the name of Fatty']
    greetOut = ['hey there!', 'hi!', 'hi there!', 'hey!']
    byeOut = ['bye bye', 'bye. see you later']


    human1 = request.form['human']
    #bot1 = request.form['bot']
    #game_id = request.form['game_id']

    if human1 in greetIn:
        bot = random.choice(greetout)
        return render_template('new.html', bot=bot)
    else:
        return

HTML code :

<html>
  <head>
    <title>BOT</title>
    <script>
        var bot = 
    </script>
  </head>
  <body>
      <h1>Hello, type something to begin!</h1>
      <form method='post'>
        Human: <input type='text' name='human'><br>
        Bot: <br>
        <input type="submit" name="action">
      </form>
  </body>
</html>

Any help would be appreciated.

Thank You!




Aucun commentaire:

Enregistrer un commentaire