jupytext icon indicating copy to clipboard operation
jupytext copied to clipboard

Possibility of inline code?

Open matthew-brett opened this issue 4 years ago • 9 comments

I continue to use Jupytext all the time, and here's another thank you for the huge improvement it has made to my work.

I know this is a fraught question, but I really miss the R notebook ability to put code outputs inline, as in:

```{python}
msg = "Hello"
```

My message is `py msg`.

Do you think there is any way of doing that?

matthew-brett avatar Apr 23 '20 14:04 matthew-brett

Thank you @matthew-brett ! Very kind of you.

Are you using Jupyter Notebook, or JupyterLab ? In the former you could use the python-markdown extension, and write

```python
msg = "Hello"
```

My message is {{msg}}.

I think this also works with nbconvert.

In JupyterLab there does not seem to be anything like this. So what people seem to be using is the following: they display Markdown text with the Markdown command, e.g.

```python tags=["hide"]
from IPython.display import Markdown
Markdown(f'''My message is {msg}.''')
```

and then they hide the input code for that cell. See for instance this blog post. Or you could also use jupyter={"source_hidden": true} according to this thread:

```python jupyter={"source_hidden": true}
from IPython.display import Markdown
Markdown(f'''My message is {msg}''' + 
         ' - the input code for this cell is hidden with a '
         'cell metadata `{"jupyter": {"source_hidden": true}}`.')
```

which gives this: Screenshot from 2020-04-24 00-07-17

mwouts avatar Apr 23 '20 22:04 mwouts

@matthew-brett also for your consideration https://myst-nb.readthedocs.io/en/latest/use/glue.html 😁

chrisjsewell avatar Apr 28 '20 12:04 chrisjsewell

@matthew-brett , did you give a try to one of the above? Which is your favorite?

mwouts avatar May 21 '20 16:05 mwouts

Thanks for the follow up. Sorry not to get back to you, I was reflecting.

I realize that my question wasn't properly considered. My use case is that I want to distribute notebooks as part of an interactive textbook; the textbook has the filled-in (executed) version, but the student downloaded the not-executed version. I think this means that neither of the two suggestions here are going to work well, because (I believe) they are both going to need the student to install extra stuff into their notebook setup.

But on reflection - I don't see how Jupytext could solve that - except perhaps by allowing something like automatic translation of this in .Rmd

Here is a variable value: `py a` 

into this in .ipynb:

Here is a variable value: {{a}}

Of course, this would have the same problem - that the .ipynb user would need to have the python-markdown extension, but then again, the user has used that non-standard syntax, so may well be prepared to take that risk.

Adding a reference to markdown-kernel here because of useful links therein.

matthew-brett avatar May 22 '20 10:05 matthew-brett

Hi @matthew-brett , well sure, we could think of translating py a in R Markdown files to {{a}} in Python Jupyter notebooks, and back. I'll think about that - it would be the first non-verbatim translation for the content of Markdown cell - but if it can help, why not?

mwouts avatar May 22 '20 13:05 mwouts

Well - I think it wouldn't help me - at the moment - because of the problem of relying on a not-standard plugin. So maybe wait until someone screams on this thread? Maybe that will be me! But not yet.

matthew-brett avatar May 22 '20 13:05 matthew-brett

Haha... Sure! Then, if you don't mind maybe I'd prefer to close this thread for now, and anyone interested can either reopen of create a new follow-up. Is that ok with you? Thanks!

mwouts avatar May 22 '20 13:05 mwouts

Fine either way. I guess, if you leave it open, you signal that you're open to negotiation on this one.

matthew-brett avatar May 22 '20 13:05 matthew-brett

I see - indeed I like your question, so let's keep this open and see what we can do on the long term. Also, I was thinking, we've discussed R Markdown and ipynb, but maybe there is something that we could for Python files with the f-strings...

mwouts avatar May 22 '20 13:05 mwouts