Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow references to Python variables in Markdown cells #2958

Closed
4 tasks
ellisonbg opened this issue Feb 20, 2013 · 53 comments
Closed
4 tasks

Allow references to Python variables in Markdown cells #2958

ellisonbg opened this issue Feb 20, 2013 · 53 comments

Comments

@ellisonbg
Copy link
Member

In PR #2592 @Carreau has come up with a syntax for referencing Python variables in Markdown cells. It uses the Jinja style {{x}} syntax. We love this idea, but there are some things that have to be worked out:

  • What syntax do we use? Are we happy with the {{}}
  • How do we make sure we are processing Markdown in a robust and sane way for latex and literate stuff. We are slowly diverging away from pure Markdown and that is really dangerous. We want the notebook format to work very broadly and having our own Markdown syntax seems like a bad idea.
  • How to handle errors, i.e., undefined variables.
  • What do we want to do about other display formats. Seems like a dangerous path to start to allow non-text formats.
@dartdog
Copy link

dartdog commented Oct 23, 2013

so,, is this dead? It would be great as the prior thread pointed out? Sorry to bump wish I had the skilz to implement ( I don't) but I sure would use...

@minrk
Copy link
Member

minrk commented Oct 23, 2013

Not dead, just need to get a few things done first.

@notionparallax
Copy link

I just asked about this on SO, only to find that it was a double duplicate from here and here.

My 2c would be that

  • Jinja syntax would be great
  • It seems from a quick look that markdown doesn't really have a 'pure', but if all the formatting conventions from a well established implementation ( :octocat: github would be a nice one) were followed then adding an injection method isn't actually a markdown issue.
  • could errors be a user configurable thing? An option of [invisible (nothing happens), the word error, or a dirty great stack trace ]
  • As long as it's a text response then it could be injected before it is passed to the markdown renderer, so it could inject html or even more markdown!

@payne92
Copy link
Contributor

payne92 commented Feb 28, 2014

Not to advocate any particular syntax (I think {{}} is fine), but here's comparable design for R: http://www.rstudio.com/ide/docs/authoring/using_markdown (They use ``` as the delimiter).

Lunamark uses <!--@ for "metadata", see: http://jgm.github.io/lunamark/lunamark.1.html

@gauteh
Copy link

gauteh commented Jun 4, 2014

👍

@bilderbuchi
Copy link

👍, this would be a very useful feature!

@jankatins
Copy link
Contributor

It would be really nice to have the same syntax as http://blog.rstudio.org/2014/06/18/r-markdown-v2/

@bilderbuchi
Copy link

I don't know, using the R-style delimiters, i.e. single and triple backticks, respectively, it seems we couldn't choose between just displaying code (like some code snippet/example), and evaluating code and displaying the result (e.g. my_var) - I don't think this is very practical (if I understand it correctly).

@payne92
Copy link
Contributor

payne92 commented Jun 23, 2014

The use case is writing notebook prose with embedded computed values, such as "The average unemployment for Morgan County in 2014 was 8.4%, down from 10.3% in 2009." In this example, you could imagine the percentages and years being calculated from data.

The broader use case is "smart documents", where you have text and graphics automatically (re) generated (perhaps hiding the code for readability) from data.

I think it's a useful feature.

@jankatins
Copy link
Contributor

I really like the "knitr" model in rstudio (one markdown document, code blocks get interpreted, values are available in markdown -> gets converted into PDF/html/... which I can publish as an journal article. Different to a notebook as the notebook is based on multiple cells and I'm not sure how to influence whether output is shown or not).

I don't mind how the values are included in the text, but I would find it nice if both R (with knitr/rstudio) and ipython would use the same.

@gauteh
Copy link

gauteh commented Jun 24, 2014

Or something similar to ruby or bash, where #{varname or statement} can be
mixed within the string.
23. juni 2014 20:27 skrev "Jan Schulz" notifications@github.com følgende:

I really like the "knitr" model in rstudio (one markdown document, code
blocks get interpreted, values are available in markdown -> gets converted
into PDF/html/... which I can publish as an journal article. Different to a
notebook as the notebook is based on multiple cells and I'm not sure how to
influence whether output is shown or not).

I don't mind how the values are included in the text, but I would find it
nice if both R (with knitr/rstudio) and ipython would use the same.


Reply to this email directly or view it on GitHub
#2958 (comment).

@bilderbuchi
Copy link

I realize the use case of interpreted code of course, I'm just concerned that we would lose markdown's ability to "just" display code (without interpreting/evaluating it), like this, if the R syntax (i.e. backticks) were chosen.

@jankatins
Copy link
Contributor

I think in knitr/R markdown you can indicate whether you want to display code (properly highligted, etc) or only the output (plots, tables,...).

I'm coming from an economics background and I don't want to see any code in my papers, so this is a bit different to the (in my opinion) optimized usecase of showing off code.

@payne92
Copy link
Contributor

payne92 commented Jun 24, 2014

@bilderbuchi oh sorry, I misinterpreted the context of your "don't think this is useful" comment.

And I agree: whatever's implemented should not break existing markdown.

@tlnagy
Copy link

tlnagy commented Aug 4, 2014

This would be wonderful and make IPython eat knitr's lunch.

@jankatins
Copy link
Contributor

https://github.com/ipython-contrib/IPython-notebook-extensions/wiki/python-markdown

I really like this approach: it takes a cell and preprocess it to valid markdown, so no changes to the markdown processor are needed.

@dartdog
Copy link

dartdog commented Aug 17, 2014

very nice...will use soon

On Sun, Aug 17, 2014 at 2:18 PM, Jan Schulz notifications@github.com
wrote:

https://github.com/ipython-contrib/IPython-notebook-extensions/wiki/python-markdown


Reply to this email directly or view it on GitHub
#2958 (comment).

Tom Brander
http://tombrander.com -Real Estate
http://oswco.com -Open Software
3763 West Jackson Blvd.
Birmingham, AL 35213
Phone 205-267-1089
@dartdog

@tlnagy
Copy link

tlnagy commented Aug 19, 2014

👍 elegant. I will use as well.

@nehalecky
Copy link

@JanSchulz, thanks! I really look forward to trying out python-markdown.

@minrk minrk removed the prio-medium label Jan 14, 2015
@sglyon
Copy link

sglyon commented Jan 31, 2015

Is there any chance of getting this into main ipython (jupyter)?

@Carreau
Copy link
Member

Carreau commented Feb 1, 2015

Is there any chance of getting this into main ipython (jupyter)?

Probably not soon. We need to do a lot of design in the background to have this to work.
Especially we would need an official way to extend markdown syntax instead of just inventing a new one.

@jankatins
Copy link
Contributor

Actually this (and also knitr/rmarkdown AFAIK) works by having a two stage conversion: first the replacement of any codeblocks with the output of the code and then convert the rest as standard markdown. So this is not an extension of markdown but a preprocessor for a cell.

@juhasch
Copy link
Contributor

juhasch commented Feb 1, 2015

I think the hard question is how to handle arbitrary invisible Python code that is executed from within a markdown cell. I don't think execution of code can be restricted in a useful implementation.

As long as it is a separate extension you actively have to install and activate, this is a different matter. Also, Python code only gets executed if the notebook is trusted.

I plan to add a tooltip showing the source code if you hover over the output of the python code in a markdown cell, so you can see where this came from.

@Carreau
Copy link
Member

Carreau commented Feb 1, 2015

Actually this (and also knitr/rmarkdown AFAIK) works by having a two stage conversion: first the replacement of any codeblocks with the output of the code and then convert the rest as standard markdown. So this is not an extension of markdown but a preprocessor for a cell.

Which is custom markdown. That's already what we have with mathjax. We obviously have to store the
un-preprocess markdown in case you re-run the notebook, so any tool that want to use our markdown have to deal with the custom preprocessing.
Pre or post processor, we invent our own syntax, that may, or may not conflict with what people want to do, or will do later.

I think the hard question is how to handle arbitrary invisible Python code that is executed from within a markdown cell. I don't think execution of code can be restricted in a useful implementation.

As long as it is a separate extension you actively have to install and activate, this is a different matter. Also, Python code only gets executed if the notebook is trusted.

I plan to add a tooltip showing the source code if you hover over the output of the python code in a markdown cell, so you can see where this came from.

If we do that, we could restrain to user_variable, ie return the value of user_ns key. That should prevent most execution.

@juhasch
Copy link
Contributor

juhasch commented Feb 1, 2015

If we do that, we could restrain to user_variable, ie return the value of user_ns key. That should prevent most execution.

You will also want to allow calling functions. A trivial case would be formatting the output or calling a custom repr.

@abalkin
Copy link
Contributor

abalkin commented Mar 30, 2015

I would love to see something like this implemented. It looks like the most straightforward solution is to run MD cells through a Jinja filter. This has been done before. See dexi, for example.

On the other hand, I don't think this should be enabled by default in all cells. Jinja would increase the complexity of the mark-up considerably and Jinja+Markdown should probably use different highlighting mode than plain MD. Why not implement this as a separate cell type?

@jankatins
Copy link
Contributor

I think this problem could go away, if it would be possible from code to hide/replace the code cell similar what is done for markdown currently:

-> Add a %%pymarkdown magic, which outputs a text/markdown message and indicates that the source should be invisible/replaced by the output. The magic would then simple do a s&r within the input.

[Edit: Ok, it has different problems, like no syntax highlighting and making no sense in the qtconsole...]

@juhasch
Copy link
Contributor

juhasch commented Aug 22, 2015

@jgab3103
Copy link

I didn't want to to the construct the string and put is as output as it just gets so messy having big html strings - neat idea though, I didn't even think of that. But there are definitely a couple of good solutions which do the trick - thanks!

@julius-datajunkie
Copy link

julius-datajunkie commented Apr 6, 2016

I think this feature is really important to have for someone who comes from Rmarkdown. R has done a much better job in creating nice reports.

@gnestor
Copy link
Contributor

gnestor commented Apr 27, 2017

I saw a duplicate of this issue in notebook. This is already possible using IPython.display.Markdown in a code cell:

image

from IPython.display import Markdown

one = 1
two = 2
three = one + two

Markdown("# Title")
Markdown("""
# Math
## Addition
Here is a simple addition example: {one} + {two} = {three}
""".format(one=one, two=two, three=three))

@ellisonbg
Copy link
Member Author

ellisonbg commented Apr 27, 2017 via email

@gnestor
Copy link
Contributor

gnestor commented Apr 27, 2017

@ellisonbg That would be nice and I think this is the right way to do it (rendering markdown from Python vs. extending marked and other markdown machinery in ways that it maybe shouldn't be extended). @Carreau Any interest in something like this?

Additionally, combining this with the new cell tags feature in notebook (see the notebook 5.0 release post), you should be able to hide input cells when converting a notebook using nbconvert (e.g. using nbconvert-hide tag). @takluyver Does the nbconvert-hide tag currently work with nbconvert?

@takluyver
Copy link
Member

I don't think it does yet, but I think we should add some nbconvert tags.

@ellisonbg
Copy link
Member Author

ellisonbg commented Apr 27, 2017 via email

@Carreau
Copy link
Member

Carreau commented Apr 27, 2017

@Carreau Any interest in something like this?

This way of doing things was already discussed in 2013 something and AFAICT was frowned upon as:

  • you need to reimplement %%markdown in every language
  • it requires computation on kernel side to display text
  • it changes the semantic meaning of rendering a markdown.
  • screw-up nbconvert --to markdown and nbconvert --to script
  • screw-up notebooks import hooks.

That's breaking semantics of the notebook (IMHO), because then the text of the "markdown" cells depends on kernel state. So while it is cute i'm going to downvote this idea.

Also the %%markdown, %%latex, %%javascript ... etc magic were written at the time when we were having this discussion.

I'll note that similar magics have existed for quite some time and way more advanced features than just using %%markdown, so please don't rush because you'r exited as you've never encountered this idea before.

@ellisonbg
Copy link
Member Author

ellisonbg commented Apr 27, 2017 via email

@gnestor
Copy link
Contributor

gnestor commented Apr 27, 2017

@jasongrout
Copy link
Member

I think this is the right way to do it (rendering markdown from Python

Just clarifying since I initially read your statement wrong - this is using the kernel to display markdown (as a markdown mimetype), not to render markdown to html. The actual rendering markdown to html is still being done on the client side, in nbconvert, etc.

I like this approach. If you want easy markdown that involves kernel state, then use the kernel to display that markdown. If you want that markdown to update live, then use a widget (you could use an OutputWidget, or we could make a MarkdownWidget similar to the HTMLWidget). This, accompanied with easy ways to hide/show input and output, makes a lot of sense to me.

@BoPeng
Copy link

BoPeng commented Aug 7, 2017

Just came across this ticket and would like to mention that SoS also took the aforementioned approach, namely rendering markdown from the kernel. Basically, sos implements a %render magic that renders the stdout of a cell in MD (by default), HTML etc, and because sos supports different languages, it also renders output from whatever subkernel it starts.

@JMLourier
Copy link

Here is a simple fix: jupyter/nbconvert#320

@abalter
Copy link

abalter commented Apr 27, 2020

It would be nice if whatever happens can also be compatible with RMarkdown. That uses

The value of x is `r x`.

Where x is a variable in R.

@BoPeng
Copy link

BoPeng commented Apr 27, 2020

https://github.com/vatlab/markdown-kernel is not necessarily what you need but it supports Rmarkdown cells in a Jupyter environment.

@adamgit
Copy link

adamgit commented Sep 30, 2020

TL;DR: is this thread still up-to-date? JupyterLab has no direct solution, but Notebook does? (2020)

So ... reading this thread, and trying the various hacks suggested ... it seems that as of 2020 this feature is still impossible/missing in JupyterLab (which the main site has been pushing people to use for several years now?), unless you use SoS (all the other workarounds seem to be either notebooks-only i.e. legacy/useless for modern installs ... or require throwing-away most of the convenience of using Jupyter's "code + markdown" core functionality).

Is there something I've missed? It seems like the main JupyterLab (note: not Notebooks, which work fine, and I feel a bit betrayed that the main project pushes Lab so hard when Notebooks clearly work a lot better, even now in 2020) essentially doesn't support markdown in docs for anything beyond trivial, non-analysis, examples - rhetorically speaking: what's the point of including a formatting system in a data analysis product if you can't output your data with the formatting?

This seems so wrong that I think I must have missed something basic. "SoS + markdown-kernel" feels like a sledgehammer to crack a walnut, but since it works ...

@gbrault
Copy link

gbrault commented Jul 22, 2021

Reading this thread, I am not sure this feature is likely to be implemented in Jupyter Lab or not.

@falknerdominik
Copy link

Is this dead? What is the reason we can not just reference it in a similar style as knitr? python variable?

@Carreau
Copy link
Member

Carreau commented Dec 12, 2023

Closing as this issue is not in IPython itself and if still problematic and relevant should be opened on the right repository if this is an actual bug or code proposal. For general help use discourse. This will allow to keep the number of opened issue on the IPython repo under control and allow you to get faster response.

@Carreau Carreau closed this as completed Dec 12, 2023
@ipython ipython locked as off-topic and limited conversation to collaborators Dec 12, 2023
@krassowski
Copy link
Member

To give some specific references on where the discussion continues:

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests