首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Python Tutor + Google Colab

Python Tutor + Google Colab
EN

Stack Overflow用户
提问于 2020-01-22 22:42:57
回答 1查看 698关注 0票数 3

我正在尝试使用Google来教学生Python。问题是,没有一个很好的工具来可视化代码执行。我尝试使用Python,将它与Google集成,即创建了一个“魔术命令”。

代码语言:javascript
复制
from IPython.core.magic import  Magics, magics_class, cell_magic, line_magic

@magics_class
class Helper(Magics):

  def __init__(self, shell=None,  **kwargs):
    super().__init__(shell=shell, **kwargs)

  @cell_magic
  def debug_cell_with_pytutor(self, line, cell):
    import urllib.parse
    url_src = urllib.parse.quote(cell)
    str_begin = '<iframe width="1000" height="500" frameborder="0" src="https://pythontutor.com/iframe-embed.html#code='
    str_end   = '&cumulative=false&py=3&curInstr=0"></iframe>'
    import IPython
    from google.colab import output
    display(IPython.display.HTML(str_begin+url_src+str_end))

get_ipython().register_magics(Helper)

以后可以用作

代码语言:javascript
复制
%%debug_cell_with_pytutor

total_list = []
for i in range(3):
  total_list.append(i)
total_list

这个问题。实际上有两个问题:

  • Python经常拒绝处理不那么复杂的代码,比如创建一个只有几个方法的类。另一方面,我能够在本地启动Python,但它没有失败。
  • 我不满意两种不同的Pythons被使用的事实--一种在Colab,另一种在Python

我在寻求什么帮助。,我想要在Google中启动Python,或者找到在Colab中可以工作的任何其他好的可视化工具。

提前谢谢你!

更新

最后,我找到了一些解决办法,但这是相当混乱的。如果有人提出更好的建议,我会很高兴的。

目前,代码看起来就像

代码语言:javascript
复制
#@title #Helper functions (debugger)

!curl -O https://raw.githubusercontent.com/fbeilstein/machine_learning/master/pytutor/data_begin.html
!curl -O https://raw.githubusercontent.com/fbeilstein/machine_learning/master/pytutor/data_end.html
!curl -O https://raw.githubusercontent.com/fbeilstein/machine_learning/master/pytutor/pg_encoder.py
!curl -O https://raw.githubusercontent.com/fbeilstein/machine_learning/master/pytutor/pg_logger.py

from IPython.core.magic import  Magics, magics_class, cell_magic, line_magic

@magics_class
class Helper(Magics):

  def __init__(self, shell=None,  **kwargs):
    super().__init__(shell=shell, **kwargs)
    with open('data_begin.html', 'r') as f_in:
      lines = f_in.readlines()
      self.begin_debug = "".join(lines)
    with open('data_end.html', 'r') as f_in:
      lines = f_in.readlines()
      self.end_debug = "".join(lines)

  @cell_magic
  def debug_cell_with_pytutor(self, line, cell):
    import json
    import bdb
    from pg_logger import PGLogger
    import IPython
    from google.colab import output

    def cgi_finalizer(input_code, output_trace):
      ret = dict(code=input_code, trace=output_trace)
      json_output = json.dumps(ret, indent=None) # use indent=None for most compact repr
      display(IPython.display.HTML("<html>" + self.begin_debug + json_output + self.end_debug + "</html>"))


    logger = PGLogger(cumulative_mode=False,
                  heap_primitives=False, 
                  show_only_outputs=False, 
                  finalizer_func=cgi_finalizer,
                  disable_security_checks=True,
                  allow_all_modules=True,
                  probe_exprs=False)

    try:
      logger._runscript(cell)
    except bdb.BdbQuit:
      print("INTERNAL ERROR OCCURED")
    finally:
      logger.finalize()

## use ipython load_ext mechanism here if distributed
get_ipython().register_magics(Helper)

帮助文件存储在我的ML课程的GitHub存储库中。我根据GraphTerm的想法和来自PyTutor存储库的资源创建了它们。您可以在第二课"Python“中找到一些用法的例子,但是有相当多的材料,因此可能需要一些时间才能找到它们。

EN

回答 1

Stack Overflow用户

发布于 2022-05-05 04:26:09

使用前面的%%debug_cell_with_pytutor尝试现有代码(以下部分):

代码语言:javascript
复制
from IPython.core.magic import  Magics, magics_class, cell_magic, line_magic

@magics_class
class Helper(Magics):

  def __init__(self, shell=None,  **kwargs):
    super().__init__(shell=shell, **kwargs)

  @cell_magic
  def debug_cell_with_pytutor(self, line, cell):
    import urllib.parse
    url_src = urllib.parse.quote(cell)
    str_begin = '<iframe width="1000" height="500" frameborder="0" src="https://pythontutor.com/iframe-embed.html#code='
    str_end   = '&cumulative=false&py=3&curInstr=0"></iframe>'
    import IPython
    from google.colab import output
    display(IPython.display.HTML(str_begin+url_src+str_end))

get_ipython().register_magics(Helper)
票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59869287

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档