1

In my HTML page, I have a button. When I click on the button it should call a Python function that takes a screenshot and save it in the same directory. I tried my Python method and it is working correctly on local. But when I used Flask to call the function from a HTML button, it does nothing and there are no errors. How to get the screenshot image ?

Python Flask:
@app.route('/')
def index():
    return render_template('index.html')

# Function to take the screenshot and save it
def takesc():
    with mss() as sct:
        sct.shot()

@app.route('/takeScreenshot')
def takeScreenshot():
    return takesc()

My HTML

<button onclick="{{ takeScreenshot }}">ScreenShoot</button>

I also tried to use 'url_for' as following, but still nothing happened:

<button onclick="{{ url_for('takeScreenshot') }}">ScreenShoot</button>
0

1 Answer 1

3

Instead of a button, it is shorter use an href:

<a href='/takeScreenshot' class='my_button_class'>ScreenShoot</a>

You can then use css to add style under the .my_button_class class.

However, to use a button, you need to specify the redirect:

<button type="button" onclick="window.location.href='{{url_for( 'takeScreenshot') }}';">Display Table Y</button>

Also, takeScreenshot is not returning text to be rendered on the screen. takesc does not return anything, thus, it should be called before the route returns a response:

@app.route('/takeScreenshot')
def takeScreenshot():
   takesc()
   return "screen shot saved"
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you so much, it worked. If you do not mind would you please tell me why I cannot use a button or what is the difference ? Also after clicking the link, the screenshot is taken but the execution ends and gives me this error 'ValueError: View function did not return a response'
Thank you so much, you are a life saver! Instead of returning "Screenshot saved" I return render("index.html") so that it does not take me to another page. Thanks again man!!!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.