How to get from input value to python and form in python

I am entering the value of the input(form) and want its value in python something like this

---------------------------HTML--------------------------------------------------

<input type="number" name="pay">
<input type="submit" name="btnpay">

----------------------------------PYTHON-----------------------------------------------------

from boltiot import Bolt
api_key = "******-****-****-****-************"
device_id  = "BOLT7420505"
mybolt = Bolt(api_key, device_id)

import cgi
form = cgi.FieldStorage()
pay = form.getvalue('pay');

if pay > 500:
    status = 'low balance :' + pay
else:
    status = pay

To accept input use ‘input()’ function.
In Python by default the input is string, so to convert to integer or floating point use int(), float() functions respectively
eg.
int_value = int(input(“enter a number”))
and so on.

1 Like

Hi @nikhildave4478,

There are two methods to resolve your issue.

1. Take the input value using input method of python. For example.

Check the example -

print("Enter pay amount:")
pay_amount = input()
print("Pay amount is, " + pay_amount)

  • Now come out from the template folder and run your code.
cd ..
python app.py

2. Take the input from HTML form.

In this case, first, you need to run a local server. To do that you can use the various python framework. For example- flask, bottle, Django (I am using flask here for showing the example. )

  • Then install flask packages.
sudo pip3 install flask
  • First, create a folder and then go inside the project.
mkdir myformproject
cd myformproject
  • Now create a python file with name app.py (You can assign any name.)
from flask import Flask, render_template, request
app = Flask(__name__)

@app.route('/',methods = ['GET'])
def show_index.html():
    return render_template('index.html')

@app.route('/send_data', methods = ['POST'])
def get_data_from_html():
        pay = request.form['pay']
        print ("Pay is " + pay)
        return "Data sent. Please check your program log"

if __name__ == '__main__':
    app.run(host="0.0.0.0", port=5000, debbug=True)
  • Inside the myformproject folder, create another folder with name templates and inside template folder create a file with name index.html.
mkdir templates
cd templates
touch index.html

and then Open index.html file and write HTML code.

<!DOCTYPE html>
<html>
  <head>
  <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.0/css/bootstrap.min.css" integrity="sha384-9gVQ4dYFwwWSjIDZnLEWnxCjeSWFphJiwGPXr1jddIhOegiu1FwO5qRGvFXOdJZ4" crossorigin="anonymous">
  </head>

  <body>
     <h1>Enter data?</h1>
    <form method="POST" action = "/send_data">
      <div class = "form-group">
        <input type="text" name = "pay">
      </div>
      <input class="btn btn-primary" type="submit" value="Pay Now">
    </form>

  </body>
</html>
  • Come out of templates folder and run your app.
cd ..
sudo python3 app.y

Note . I have not tested the code.

Do let me know in case you need any other information.

As I wanted to get both switches for on and off in one code I used this as you said input() by default for string and here I wanted to enter input as ON but after running the code i didn’t get option to enter .Can I know any mistakes in this code

from boltiot import Bolt
api_key = “xxxxxxxxxxxxxxxxxxx”
device_id =“BOLTxxxxxxxxx”
mybolt=Bolt(api_key,device_id)
print(“enter whether to switch ON or OFF”)
input = ‘input()’
if input ==“ON”:
response1 = mybolt.digitalWrite(‘0’,‘HIGH’)
print(response1)
else:
response2 = mybolt.digitalWrite(‘0’,‘LOW’)
print(response2)

In python the input() will always returns a string. If we need a input in the integer form then we can go with 2 different steps:

1st step:
Let us have a example as
value=input(“Give a number:”)
Here the type of value is string so we can write it has, value = int(value) , now value becomes the type as integer.

2nd step:
We can directly give the type in the input() as:
value=int(input(“Give a number:”))
Here the type of value is integer. In this way we can get the input in python.