srush / streambook

Live Python Notebooks with any Editor

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Longer code blocks

cgarciae opened this issue · comments

Hey Sasha,

I was wondering if its possible to avoid code blocks being split after a single empty line?
An alternative definition for a "cell" could be that it takes all code until the next comment but if you want to force the split you could insert a single #, e.g:

# The next python block ends at the next '#'
x = 1
y = 2

z = x + y

#
x += 1
y += 2

z = x + y

# This comment/markdown cell ends the previous cell as usual
a = x + y + z 

The previous should have 5 cells:

  • markdown
  • python
  • python
  • markdown
  • python

Hi Christian, Good question.

In my example I am using the jupytext "light" format by default, but in theory we could use any of the Jupytext formats. It sounds like the "percent" format might be better for your use cases.

https://github.com/mwouts/jupytext/blob/master/docs/formats.md#the-light-format

I believe you can add a header to your file that tells jupytext to use a different format. I don't think I hardcoded "light" format anywhere. Let me know if that works.

Ah sorry for being so vague, I meant the streamlit preview, here is an example:

streambook

I would be nice is code blocks where not split by empty lines since you usually don't code with everything cluttered together. I haven't checked the conversion to jupyter, maybe this is not an issue in that regard.

Oh, I should be more clear. Everything goes through the jupytext parser even the streamlit preview. That output above is the anticipated result of a light markdown format (even if you disagree with it). If you want it not to do that, you do need to switch to the percent format. That format solves this issue by marking start of new cells. It should be just one line at the top of your file.

Thanks Sasha! I will try that out :)