Search code examples
pythonflaskpythonanywhere

pyscreenshot.err.FailedBackendError: All backends failed


I am a newbie to python web apps.

I'm using pythonanywhere to host my webapp. Pyscreenshot is installed via pip3.8 install --user Pillow pyscreenshot

I am using ajax to call a function on button click which uses pyscreenshot to take screenshot of the screen and store it as an image file on given path.

Everything runs fine on local environment (manjaro, python3.8)

However when I run the same code on pythonanywhere, I get an unhandled exception on the site. In the error logs I see the following:

2020-06-16 11:30:09,927: Exception on /takess [GET]
Traceback (most recent call last):
  File "/usr/lib/python3.8/site-packages/flask/app.py", line 2446, in wsgi_app
    response = self.full_dispatch_request()
  File "/usr/lib/python3.8/site-packages/flask/app.py", line 1951, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/usr/lib/python3.8/site-packages/flask/app.py", line 1820, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "/usr/lib/python3.8/site-packages/flask/_compat.py", line 39, in reraise
    raise value
  File "/usr/lib/python3.8/site-packages/flask/app.py", line 1949, in full_dispatch_request
    rv = self.dispatch_request()
  File "/usr/lib/python3.8/site-packages/flask/app.py", line 1935, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "/home/Muhammadpen/mysite/flask_app.py", line 12, in takess
    im = ImageGrab.grab()
  File "/home/Muhammadpen/.local/lib/python3.8/site-packages/pyscreenshot/__init__.py", line 31, in grab
    return backend_grab(backend, bbox, childprocess)
  File "/home/Muhammadpen/.local/lib/python3.8/site-packages/pyscreenshot/loader.py", line 147, in backend_grab
    return auto(bbox, childprocess)
  File "/home/Muhammadpen/.local/lib/python3.8/site-packages/pyscreenshot/loader.py", line 127, in auto
    raise FailedBackendError(msg)
pyscreenshot.err.FailedBackendError: All backends failed!

Here is the code:

Index.html

<!DOCTYPE html>
<html>
  <head>
    <title>Inspector jitsi</title>
  </head>
  <script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
  <script type=text/javascript>
    $(function() {
      $('a#ssbutton').bind('click', function() {
        $.getJSON('/takess',
            function(data) {
          //do nothing
        });
        return false;
      });
    });
</script>
  <body>
    <iframe
      src="https://meet.jit.si/"
      style="height: 900px; width: 100%;"
    ></iframe>
    <div class='container'>
          <form>
              <a href=# id=ssbutton><button class='btn btn-default'>Take screenshot</button></a>
          </form>

  </div>
  </body>
  <style>
    html,
    body {
      overflow: hidden;
      margin: 0px;
      padding: 0px;
    }
    form{
      position: absolute;
      left: 50%;
      z-index: 0;
    }
  </style>
</html>

app.py:

from flask import Flask, render_template
import pyscreenshot as ImageGrab

app = Flask(__name__)

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

@app.route('/takess')
def takess():
    im = ImageGrab.grab()
    im.save(r'/home/Muhammadpen/mysite/ss1.png')
    # im.save(r'/home/pen/Documents/ss1.png')


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

I cant find any relevant info or fixes about this, so any help will be appreciated. Ty!


Solution

  • PyScreenshot is for taking screenshots of desktop applications. Since your application is running on a server, there's nothing to take a screenshot of.

    If you intended to take a screenshot of the user's browser, you would use frontend tooling, which runs in the browser, not a server-side tool.