在Python函数中运行Javascript代码

在Python函数中运行Javascript代码,javascript,python,syntax-error,Javascript,Python,Syntax Error,我试图在Colab中的python函数中运行javascript代码。这是我的代码(要查找的主要块是ThisstartStreamUpload函数)。顺便说一句,我对Javascript还不太熟悉,只是在某个时候被迫使用它 def sendConnOffer(port): ''' Make POST req from JS to the connected VM (server). Sends the WebRTC stream offer in POST req ''' js

我试图在Colab中的python函数中运行javascript代码。这是我的代码(要查找的主要块是This
startStreamUpload
函数)。顺便说一句,我对Javascript还不太熟悉,只是在某个时候被迫使用它

def sendConnOffer(port):
  '''
  Make POST req from JS to the connected VM (server). Sends the WebRTC stream offer in POST req
  '''
  js = Javascript('''
    // START JS code
    /** 
     * prints the logs to cell output inside notebook
     */
    function printLog (text) {
      document.querySelector("#output-area").appendChild(document.createTextNode(text));
    }

    // peer connection
    let pc = null;
    /**
     * creates a peer connection, opens the device streams then calls negotiate() to connect with server
     * @param serverUrl: url of other peer. Server in this case
     */
   // HERE // async function startStreamUpload(serverUrl) {
      const connectionConfig = { sdpSemantics: 'unified-plan' };
      pc = new RTCPeerConnection(connectionConfig);
      
      const streamConfig = {
        audio: false,
        video: { width: 640, height: 480 }
      };
      let stream = await navigator.mediaDevices.getUserMedia(streamConfig); // access the webcam stream
      stream.getTracks().forEach((track) => {
        pc.addTrack(track, stream); // tracks can be video/audio. Video in our case
      });

      // negotiate the deal with server
      await negotiate(serverUrl);
    }

   .......
   .......
   .......
  
  """)
  
  
  display(js) # make the provided HTML, part of the cell
  # the server running in Colab VM
  serverUrl = f"https://localhost:{port}/"
  print("ServerURL: ", serverUrl)
  # call the startStreamUpload() JavaScript function
  jsFuncToCall = eval_js('startStreamUpload({})'.format(serverUrl))
  print("jsFuncToCall: ", jsFuncToCall)
  return 


sendConnOffer(1234)
这是完整的错误消息:

     89   print("ServerURL: ", serverUrl)
     90   # call the startStreamUpload() JavaScript function
---> 91   jsFuncToCall = eval_js('startStreamUpload({})'.format(serverUrl))
     92   print("jsFuncToCall: ", jsFuncToCall)
     93   return

/usr/local/lib/python3.7/dist-packages/google/colab/output/_js.py in eval_js(script, ignore_result, timeout_sec)
     38   if ignore_result:
     39     return
---> 40   return _message.read_reply_from_input(request_id, timeout_sec)
     41 
     42 

/usr/local/lib/python3.7/dist-packages/google/colab/_message.py in read_reply_from_input(message_id, timeout_sec)
    104         reply.get('colab_msg_id') == message_id):
    105       if 'error' in reply:
--> 106         raise MessageError(reply['error'])
    107       return reply.get('data', None)
    108 

MessageError: SyntaxError: missing ) after argument list

任何建议…

您需要在
startStreamUpload
的参数周围加引号,因为它是一个字符串

jsFuncToCall = eval_js('startStreamUpload("{}")'.format(serverUrl))

你做了什么来尝试和确定错误指的是什么?你能显示完整的错误吗。它给出了一行吗?你用三重单引号开始JS,但以三重双引号结束。@Barmar修复了它。但是仍然没有结果。
//这里//
注释掉了
startStreamUpload
函数定义的开头。谢谢,但现在在同一行中发生了另一个错误:
JSON输入的意外结束
JavaScript代码中发生了错误。Python将在
eval_js()
行中报告所有这些错误。