Categories
Python Answers

How to cast Python Flask form value to int?

Sometimes, we want to cast Python Flask form value to int.

In this article, we’ll look at how to cast Python Flask form value to int.

How to cast Python Flask form value to int?

To cast Python Flask form value to int, we call the int function.

For instance, we write

@app.route("/getpersonbyid", methods=["POST"])
def get_person_by_id():
    personId = int(request.form["person-id"])
    return str(personId)

to create the /getpersonbyid route with the get_person_by_id function.

In it, we get the form value from the form field with the name attribute set to person-id from request.form.

Then we convert the value to an int with int.

Conclusion

To cast Python Flask form value to int, we call the int function.

Categories
Python Answers

How to upload image in Flask and Python?

Sometimes, we want to upload image in Flask and Python.

In this article, we’ll look at how to upload image in Flask and Python.

How to upload image in Flask and Python?

To upload image in Flask and Python, we can get the uploaded file from the request.files property.

For instance, we write

import os
from flask import Flask, request

UPLOAD_FOLDER = "./upload"

app = Flask(__name__)
app.config["UPLOAD_FOLDER"] = UPLOAD_FOLDER


@app.route("/", methods=["GET", "POST"])
def upload_file():
    if request.method == "POST":
        if "file1" not in request.files:
            return "there is no file1 in form!"
        file1 = request.files["file1"]
        path = os.path.join(app.config["UPLOAD_FOLDER"], file1.filename)
        file1.save(path)
        return "ok"
    return """
    <h1>Upload new File</h1>
    <form method="post" enctype="multipart/form-data">
      <input type="file" name="file1">
      <input type="submit">
    </form>
    """


if __name__ == "__main__":
    app.run()

to add the upload_file function that’s mapped to the / route.

In it, we check for the file uploaded with the request.files dictionary.

We check for the file with key file1 with "file1" not in request.files.

If it exists, then we get the file’s path with

os.path.join(app.config["UPLOAD_FOLDER"], file1.filename)

Then we save the file with file1.save(path).

If the request method isn’t 'POST', then we show use the file upload form to let them upload the file.

Conclusion

To upload image in Flask and Python, we can get the uploaded file from the request.files property.

Categories
Python Answers

How to set response headers in Flask and Python?

Sometimes, we want to set response headers in Flask and Python.

In this article, we’ll look at how to set response headers in Flask and Python.

How to set response headers in Flask and Python?

To set response headers in Flask and Python, we set the headers property of the response object.

For instance, we write

@app.route("/")
def home():
    resp = make_response("hello")
    resp.headers["Access-Control-Allow-Origin"] = "*"
    return resp

to call make_response to create response object that returns a string response.

Then we set the Access-Control-Allow-Origin header with

resp.headers["Access-Control-Allow-Origin"] = "*"

Finally, we return the resp object in the home route.

Conclusion

To set response headers in Flask and Python, we set the headers property of the response object.

Categories
Python Answers

How to run JavaScript in Selenium using Python?

Sometimes, we want to run JavaScript in Selenium using Python.

In this article, we’ll look at how to run JavaScript in Selenium using Python.

How to run JavaScript in Selenium using Python?

To run JavaScript in Selenium using Python, we can use the execute_script method.

For instance, we write

from selenium import webdriver

driver = webdriver.Firefox()
driver.get("http://example.com")
driver.execute_script("document.getElementByElement('foo').click()")

to call driver.get to open the http://example.com page.

Then we call execute_script with "document.getElementByElement('foo').click()" to run "document.getElementByElement('foo').click()".

Conclusion

To run JavaScript in Selenium using Python, we can use the execute_script method.

Categories
Python Answers

How to get the return value of JavaScript code in Python Selenium?

Sometimes, we want to get the return value of JavaScript code in Python Selenium.

In this article, we’ll look at how to get the return value of JavaScript code in Python Selenium.

How to get the return value of JavaScript code in Python Selenium?

To get the return value of JavaScript code in Python Selenium, we get the return value of the execute_script method.

For instance, we write

from selenium import webdriver

wd = webdriver.Firefox()
wd.get("http://localhost/foo/bar")
val = wd.execute_script("return 5")

to call execute_script to run the "return 5" JavaScript code.

Then we get the returned value from val, which is assigned the return value of execute_script.

Therefore, val is 5.

Conclusion

To get the return value of JavaScript code in Python Selenium, we get the return value of the execute_script method.