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>
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"
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments