Pls add pyautogui library

Hi,
Pls add pyautogui library I need it for screen recording.

Um… "adding pyautogui" means adding it to the set of packages that you can import in server-side code only. I believe that server-side code does not generally run with a screen attached, so there is nothing there, on Anvil’s servers, for you to record.

To try to determine a reasonable solution, can you tell us, whose screen did you want to record? Your own screen? Your app’s end-user’s screen?

Hi,
I want to record a video on the client’s device. That’s why I’m trying to run the modules in the client code.

Just for clarity, are you recording the video through their device’s camera or recording the users screen?

I’m using the device’s camera.

I don’t think this approach will be quite what you intend. pyautogui won’t be available on the client side because it’s not supported in the browser. And I can’t see that it is useful in a server module for this context.

One approach for an anvil app that does device/screen recording is to dive into anvil.js on the client side. Here is a clone. I adapted the source code from a post on dev.to

Related documentation:

Anvil Docs | Using JavaScript

Anvil Docs | Client Code

For those who prefer reading code:


from anvil.js.window import navigator, MediaRecorder, URL, Blob

class Form1(Form1Template):
  def __init__(self, **properties):
    self.init_components(**properties)

  def start_recording(self):
    self.stream = navigator.mediaDevices.getUserMedia({'video':{'facingMode': 'environment'}})
    # other options: 'audio': True, 'video': True, 'video': {'facingMode': 'user'}
    self.recorder = MediaRecorder(self.stream)
    self.chunks = []
    
    def ondata(e):
      self.chunks.append(e.data)
    
    def onstop(e):
      blob = Blob(self.chunks, {'type': self.chunks[0].type})
      self.video_1.src = URL.createObjectURL(blob)
      # self.video_1 is a custom html component with a video html tag
    
    self.recorder.ondataavailable = ondata
    self.recorder.onstop = onstop
    self.recorder.start()
    
  def stop_recording(self):
    self.recorder.stop()
    self.stream.getVideoTracks()[0].stop()
    
    
  def start_btn_click(self, **event_args):
    """This method is called when the button is clicked"""
    self.start_recording()
    self.start_btn.enabled = False
    self.stop_btn.enabled = True

  def stop_btn_click(self, **event_args):
    """This method is called when the button is clicked"""
    self.stop_recording()
    self.start_btn.enabled = True
    self.stop_btn.enabled = False

1 Like

Another (probably better) approach might be to use the file uploader component. On a mobile device this will allow the user to take photo or video or use their photo library in the usual way.

image

5 Likes