Integrate a digital assistant into your Flask website with Python.
index.html code:
-----------------------
All you need is :
<h1> What is your question? </h1>
<form action='/' method='post'>
<input placeholder='Your message here' name='post' class="chaty">
<input type='submit' value='Send message' class="ruby">
</form>
And the answer :
<div class="maindiv"><b><p>Your Answer might be: {{ solution }}</b> </div>
</p>
<br />
redline.py code:
-----------------------
import wolframalpha
global solution
solution = ["solution:"]
def red_line(question):
try:
#
c = wolframalpha.Client('YOUR API HERE')
res = c.query(question)
solve = next(res.results).text
solver = solve
solution.append(solver)
except StopIteration:
solution.append("Im sorry i do not know that.")
except AttributeError:
solution.append("Im sorry i do not know that.")
server.py code:
------------------------
from flask import Flask, render_template, request
from flask_cors import CORS
import importlib
import os
app = Flask(__name__)
CORS(app)
@app.route('/', methods=['GET', 'POST'])
def index():
if request.method == "GET":
pass
elif request.method == "POST":
post = request.form.get('post')
#print(str(post))
q = str(post)
import redline
importlib.reload(redline)
from redline import red_line
line = red_line(q)
from redline import solution
solution = str(solution[1])
print(solution)
return render_template('index.html', solution=solution)
#os.system("dir > dir.txt") - OK so our dir is okay.
return render_template('index.html')
if __name__ == '__main__':
app.run(debug=True, host="192.168.1.106",port =80)
----------------------------
How does this work?
-server runs and takes input from our html (main website , the first one you load has an input box- they are called FORMS)
after it takes input it passes it trough the program, feeds it to our redline.py,
redline.py executes our search trough wolframalpha and returns an answer to our server.py.
After that server writes the answer onto the website.
Thank you so much for visiting.
Have a nice day :).
-HOX
Comments