小编For*_*ver的帖子

Jupyter Notebook 中的 Google Colabs eval_js?

google.colab.output.eval_js有什么方法可以获取Jupyter Notebook 中的行为吗?我有一些代码用来IPython.display让用户绘制图像。当按下按钮时,JS 将图像内容保存为data,然后我data = eval_js("data")在 Python 中使用该内容将该内容提取到 Python 中。我正在尝试在 Jupyter Notebook 中复制这种行为。我把完整的代码放在下面。


from IPython.display import HTML, Image
from google.colab.output import eval_js

canvas_html = """
<canvas width=%d height=%d style="background-color:rgb(240,240,240)"=></canvas>
<button>Guess Number</button>
<script>
var canvas = document.querySelector('canvas')
var ctx = canvas.getContext('2d')
ctx.lineWidth = %d
var button = document.querySelector('button')
var mouse = {x: 0, y: 0}
canvas.addEventListener('mousemove', function(e) {
  mouse.x = e.pageX - this.offsetLeft
  mouse.y = e.pageY - this.offsetTop
})
canvas.onmousedown = ()=>{
  ctx.beginPath()
  ctx.moveTo(mouse.x, …
Run Code Online (Sandbox Code Playgroud)

javascript python jupyter-notebook google-colaboratory

6
推荐指数
1
解决办法
5413
查看次数