簡體   English   中英

Python 導師 + Google Colab

[英]Python Tutor + Google Colab

序言。 我正在嘗試使用 Google Colab 來教學生 Python。問題是,沒有很好的工具來可視化代碼執行。 我嘗試通過將 Python Tutor 與 Google Colab 集成來使用它,即創建了一個“魔術命令”

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)

以后可以用作

%%debug_cell_with_pytutor

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

問題。 其實有兩個問題:

  • Python 導師經常拒絕處理不那么復雜的代碼,比如用很少的方法創建 class。 另一方面,我能夠在本地啟動 Python Tutor 並且沒有失敗。
  • 我對使用兩種不同的 Python 感到不滿意——一種在 Colab 中,另一種在 Python Tutor 中

我在尋求什么幫助。 我想在 Google Colab 中啟動 Python Tutor,或者尋找可以在 Colab 中使用的任何其他不錯的可視化工具。

先感謝您!

更新

在一天結束時,我找到了一些解決方案,但它非常混亂。 如果有人提出更好的建議,我會很高興。

目前代碼看起來像

#@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)

幫助程序文件存儲在我的 GitHub 機器學習課程存儲庫中。 我根據GraphTerm 的想法和來自PyTutor 存儲庫的資源創建了它們。 在第二講“Python”中可以找到一些使用示例,但是資料比較多,可能需要一些時間才能找到。

在它之前使用%%debug_cell_with_pytutor嘗試您現有的代碼(以下部分):

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)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM