0
votes

im doing this project which requires me to submit some forms and then return the value. However when i press the submit button the form disappear like as if the page got refreshed. Can any one help me? the code is in HTML

<div id="b2" class="containerTab" style="display:none;background:white">
  <span onclick="this.parentElement.style.display='none'" class="closebtn">x</span>
 <form method="POST">
  <span style="float: left"><b>BTC amt >=: </b></span><center><b> Depth: <input type="range" name="rangeInput" min="0" max="20" onchange="updateTextInput2(this.value);"><input style="font-size:15px;" type="text" id="textInput2" value=""></b></center>
  <input style="font-size:15px;" type="text" name=BTC amt><br>
  <b>And <= :</b><br>
  <input style="font-size:15px;" type="text" name="mdA">
   &emsp;
  <input style="font-size:20px;" name="BTCamt"  type="submit" value="Submit"><br><br><br><br>
</form>
  <div>
  {{outBTC}}
  </div>
</div>

This is the function im trying to run in this HTML

from flask import Flask, render_template, url_for, request app = Flask(name)

@app.route("/")
def home():
    return render_template('home.html')

@app.route("/", methods=['POST'])


def index():
    # if form.validate_on_submit():
    if 'transactionid' in request.form:
        transactionaddr = request.form['transactionid']
        newresult = runCypher(transactionaddr)
        return render_template('home.html', outputresult=newresult)
    elif 'BTCamt' in request.form:
        transactionaddr = request.form['BTCamt']
        newresult = runCypher(transactionaddr)
        return render_template('home.html', outBTC=newresult)

def runCypher(transactionaddr):
    from neo4j import GraphDatabase
    uri = "bolt://localhost:7687"
    user = "neo4j"
    password = "123"
    graphdb = GraphDatabase.driver(uri, auth=(user, password))
    session = graphdb.session()
    q1 = 'MATCH g=(n:out {addr: "'+transactionaddr+'"})-[*..3]-(m) RETURN g'
    nodes = session.run(q1)
    out = ""
    for node in nodes:
        out += str(node)

    return out


if __name__ == '__main__':
    app.run(debug=True)
1
why the java tag?Karthikeyan Vaithilingam
What do you expect? If you do not handle the submit event yourself and AJAX the request, the page will be reloaded (the action of the form is empty, so redirected to same page or whatever page you redirect to on the server)mplungjan
@mplungjan im trying to get the function output from the python file that is link to this HTML file and display that result.Qi Nan Sun

1 Answers

0
votes

The / route was declared twice. This should point you in the right direction..

@app.route("/", methods=['GET','POST'])
def index():
    # if form.validate_on_submit():
    if request.method == 'POST':
      if 'transactionid' in request.form:
          transactionaddr = request.form['transactionid']
          newresult = runCypher(transactionaddr)
          return render_template('home.html', outputresult=newresult)
      elif 'BTCamt' in request.form:
          transactionaddr = request.form['BTCamt']
          newresult = runCypher(transactionaddr)
          return render_template('home.html', outBTC=newresult)

    return render_template('home.html')