ipython / ipython/ipython

Automatic creation of new HEADING/MARKDOWN cells via widgets

未關閉
#11,581 1 則留言 0 個 reaction 已指派 0 人 在 GitHub 檢視
主要語言
Python
星號
16.8k
分支
4.5k
平均合併
1 天 2 小時
30 天內合併 PR
6

描述

Hi all :)

I want to automatically create a new cell by pressing a widget button. My problem is that I want to make the new cell a type HEADING cell.

I have seen this piece of code in this forum:

`import base64
from IPython.display import Javascript, display
from IPython.utils.py3compat import str_to_bytes, bytes_to_str

def create_code_cell(code='', where='below'):
"""Create a code cell in the IPython Notebook.

Parameters
code: unicode
Code to fill the new code cell with.
where: unicode
Where to add the new code cell.
Possible values include:
at_bottom
above
below"""
encoded_code = bytes_to_str(base64.b64encode(str_to_bytes(code)))
display(Javascript("""
var code = IPython.notebook.insert_cell_{0}('code');
code.set_text(atob("{1}"));
""".format(where, encoded_code)))`

I have use it along with `from IPython.html.widgets import *
def on_click(button):
create_code_cell('print("Hello world!")')
button = ButtonWidget(description="Show hello world code")
button.on_click(on_click)
display(button)` and it works fine to create code cells.

I have been reading the display documentation but i have not been able to change the new cell type to **heading**. Is there a way to do it?

Any help will be much appreciated!

貢獻指南

開啟貢獻指南

評估

這個 Issue 還沒有評估資料。

把新 issue 寄到你的電子郵件信箱

精選適合新手參與的 GitHub issue 摘要。