Automatic creation of new HEADING/MARKDOWN cells via widgets
- Dominant language
- Python
- Stars
- 16.8k
- Forks
- 4.5k
- Avg merge
- 1d 2h
- Merged PRs (30d)
- 6
Description
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!
Contributor guide
Assessment
This issue has not been assessed yet.