# How to programmatically create a markdown cell?

**URL:** <https://discourse.jupyter.org/t/how-to-programmatically-create-a-markdown-cell/14062>\
**Category:** JupyterLab\
**Tags:** how-to, help-wanted\
**Created:** [April 28, 2022, 9:44pm UTC](https://discourse.jupyter.org/t/how-to-programmatically-create-a-markdown-cell/14062 "2022-04-28T21:44:27Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![CatChenal](https://avatars.discourse-cdn.com/v4/letter/c/3ec8ea/32.png) [@CatChenal](https://discourse.jupyter.org/u/CatChenal)\
**Post date:** [April 28, 2022, 9:44pm UTC](https://discourse.jupyter.org/t/how-to-programmatically-create-a-markdown-cell/14062/1 "2022-04-28T21:44:27Z")

</div>

I am using this function to create a preformatted html div in a new cell:

```python
def new_section(title='New section'):
    style = "text-align:center;background:#c2d3ef;padding:16px;color:#ffffff;font-size:2em;width:98%"
    div = f'<div style="{style}">{title}</div>'
    return get_ipython().set_next_input(div, replace=True)

```

I call the function `new_section()` in a new cell and after execution, the html replaces the call.  
This is only a partial solution to what I want to accomplish, which is to create that cell with a cell\_type of ‘markdown’ so that I do not have to **manually** change the cell type before running it to render the html.  
Is that possible?  
Thanks for you help.

---

<div class="post-metadata">

**Author:** ![bollwyvl](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.jupyter.org/bollwyvl/32/904_2.png) [@bollwyvl](https://discourse.jupyter.org/u/bollwyvl)\
**Post date:** [April 28, 2022, 10:05pm UTC](https://discourse.jupyter.org/t/how-to-programmatically-create-a-markdown-cell/14062/2 "2022-04-28T22:05:45Z")

</div>

The line between client and kernel is intentionally fairly austere: the linked code works today in e.g. JupyterLab and the IPython terminal app… but probably not nbconvert, for example. I would also probably not expect this to be extended in the future.

One approach to bridging the two is a client-specific tool such as [ipylab](https://github.com/jtpio/ipylab): it can run any jupyterlab command… but there might not be one for “new markdown cell”.

---

<div class="post-metadata">

**Author:** ![CatChenal](https://avatars.discourse-cdn.com/v4/letter/c/3ec8ea/32.png) [@CatChenal](https://discourse.jupyter.org/u/CatChenal)\
**Post date:** [April 28, 2022, 10:25pm UTC](https://discourse.jupyter.org/t/how-to-programmatically-create-a-markdown-cell/14062/3 "2022-04-28T22:25:36Z")

</div>

Thanks for your prompt response!  
It seems to me that the outcome of `get_ipython().set_next_input` is to create a new **CODE** cell by default.  
How difficult would it be to amend the `set_next_input` method to include a `cell_type` parameter, which would be ‘code\_cell’ by default, along with `markdown_cell` and `raw_cell` as the other valid cell types?

---

<div class="post-metadata">

**Author:** ![bollwyvl](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.jupyter.org/bollwyvl/32/904_2.png) [@bollwyvl](https://discourse.jupyter.org/u/bollwyvl)\
**Post date:** [April 29, 2022, 12:50am UTC](https://discourse.jupyter.org/t/how-to-programmatically-create-a-markdown-cell/14062/4 "2022-04-29T00:50:20Z")

</div>

put more simply: no. the kernel doesn’t know about being in a notebook.
