I have built a sudoku solver using python and today I set up a web page for the user to input numbers into a sudoku-looking grid and then click solve. This was done with a form and "solve" is the submit button. Because the sudoku page is long and the problem isn't specific to the page, I put some simplified html form code below as a test.
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<title>Test</title>
</head>
<body>
<form id="sudoku" action="Test.py" method="post">
<label for="num-input"> Enter a Number: </label>
<input type="number" name="num-input">
<label for="num-input"> Enter another Number: </label>
<input type="number" name="num-input">
<label for="num-input"> Enter a third Number: </label>
<input type="number" name="num-input">
<button type="submit" name="submit" form="sudoku"> Submit </button>
</form>
</body>
</html>
When the form is submitted it should be sent to Test.py in order to be manipulated. On the web I have found examples using both CGI and Flask to do this, but both times I have encountered the same problem.
Adapted CGI Code from Posting html form values to python script:
import cgi
form = cgi.FieldStorage()
numbers = form.getvalue('num-input')
Adapted Flask Code from https://opentechschool.github.io/python-flask/core/form-submission.html:
from flask import request, redirect
@app.route('/Test.py', methods = ['POST'])
def signup():
nums = request.form['num-input']
print(nums)
return redirect('/index.html')
I have tried both solutions and many others on my test form and I keep having one recurring issue. When I click submit, the browser redirects me to a page where is displays the raw python code without executing any of it (or so it seems). The form shouldn't be the problem; I verified it was outputting data by briefly switching the method attribute to "get" and checking the url of the page where I was redirected. So the problem must be in the browser, or maybe in the python code? I am not very experienced in either CGI or Flask and as a matter of fact I am a beginner at coding in general, but given the amount of similar solutions on the internet for sending html form data to python files I am assuming this is meant to be quite a straightforward matter.
As an added on question, where is the output of the python code meant to go if I didn't yet place it in another html page?
Thanks in advance for any help.