ipython / ipython/ipython

Automatic creation of new HEADING/MARKDOWN cells via widgets

Offen
#11,581 1 Kommentar 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen
Vorherrschende Sprache
Python
Sterne
16.8k
Forks
4.5k
Ø Merge
1 T. 2 Std.
Gemergte PRs (30 T.)
6

Beschreibung

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!

Beitragsleitfaden

Beitragsleitfaden öffnen

Bewertung

Dieses Issue wurde noch nicht bewertet.

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.