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.

from flask import Flask, request, render_template 
  
# Flask constructor
app = Flask(__name__)   
  
# A decorator used to tell the application
# which URL is associated function
@app.route('/test.py', methods =["GET", "POST"])
def get_freq():
    if request.method == "POST":
       # getting input with freq = set_freq in HTML form
       freq = request.form.get("set_freq") # <--- do whatever you want with that value
       return "Your freq value is " + freq
    return render_template("form.html")
  
if __name__=='__main__':
   app.run()

then you will need to provide your form.html template to be served, and put it in the ./templates folder.

<form action="{{ url_for("get_freq") }}" method="post">
<label for="set_freq">Set Frequency:</label>
<input type="number" id="set_freq" name="set_freq" >
<button type="submit">submit</button>

@b.aarthivarshini @rutvikchoudhari14

x=input(“enter a value”)
print(x)

To get an input value in Python, you can use the input() function. It prompts the user for input and returns the entered value as a string. You can then assign this value to a variable for further processing. For example:
user_input = input("Enter a value: ")

To create a form in Python, you can use web frameworks like Flask or Django. These frameworks provide tools for handling HTTP requests and rendering HTML forms. You can define form fields, validations, and actions in your Python code, which is executed when the form is submitted. The framework takes care of routing the request, validating the input, and returning a response. The specific implementation details depend on the framework you choose.
Explore the Python Course in Pune to embark on your journey in the field of development and kickstart your career.

If a user wants to get an input value in Python, we can use an input function as input().
Then we want an integer as an input so we can use int(input()) function. Because input() function gets only string datatype only. we want to specify the datatype while getting input except string.

To take input from the user in Python, you can use the built-in function input(). The input() function allows you to prompt the user for input and receive the entered value as a string. Here’s a simple example:

# Taking input from the user
name = input("Enter your name: ")

# Displaying the input
print("Hello, " + name + "! Welcome to Python!")

# Taking numerical input
age = int(input("Enter your age: "))

# Performing calculations with the input
year_of_birth = 2023 - age
print("You were born in the year", year_of_birth)

In this example, the input() function is used to prompt the user for their name and age. The input() function returns a string, so if you want to take numerical input and use it for calculations, you may need to convert it to the appropriate data type, like we did with int(input(...)).

When you run this code, it will display prompts to the user, and they can enter their name and age. The program will then process the input and provide appropriate output based on the entered values.

Keep in mind that the input() function will always return a string, so if you need to handle different data types, you’ll need to use type casting (e.g., int(), float(), etc.) to convert the input to the desired data type.