-
-
Notifications
You must be signed in to change notification settings - Fork 4.4k
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
Literate programming #2592
Literate programming #2592
Conversation
@Carreau: Can you answer a naive question... I don't know anything about In your example above, is the value of Would it be easy to extend the notebook viewer to support this? |
Also, in US English I think it would be literate, not literrate. |
I store the repr in metadata.
Yes, that would be easy. I'm just thinking of extending Also nbviewer ships with a modified version of nbconvert, so I would like nbconvert to support conversion without going through files to dump the modified version which is a pain to maintain. |
I was too lazy to look for it, I'll fix that. Thanks. |
var text = this.get_text(); | ||
if (text === "") { text = this.placeholder; } | ||
text = IPython.mathjaxutils.remove_math(text); | ||
var literateRegex = /::([a-z_0-9]+)::/ig, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This isn't the correct regular expression for Python variables. They can't start with numbers.
There's also Python 3 Unicode variables, but I don't think anyone will blame you if you don't support those :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, also one can want to show the value of a dict, or a list, or an object property....
Why do you use |
Because that's what I've started to think at how to extend user_varibles here |
literrate -> literate |
}; | ||
|
||
|
||
MarkdownCell.prototype = new TextCell(); | ||
|
||
MarkdownCell.prototype.set_litterate_data = function (data) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
litterate -> literate here as well
also, this doesn't seem to work for me (Chromium 22.0.1229.94 Debian wheezy/sid (161065)) |
ah.. I may have forgot to push one commit.
There is no reason for it not to work... what does it not do ? |
Fixed and rebase to have the introduction of the example notebook in a separate commit. I use the same version of chrome without problem, I don't see what could cause it not to work. |
It worked for me back when I tested it. |
So I have a question: after the variable is replaced in a markdown cell, can it ever be updated if the kernel variable change, or do you have to go back and retype it later? |
You have to edit and re-render the Markdown cell. It's a little awkward since you can't execute Markdown cells like you can code cells. |
But you have to remove to old kernel value, re-add the literate |
No, just just have go go into edit mode and back. Once Code Mirror is shown even for a split second, the cell is marked as "dirty" and request new value to the kernel when re-rendered. (it is actually rendered once with empty placeholder and re-render one more time when values come back from the kernel). From the few test I did "run all" should also update the MD cell, and in the right order as the MD kernel execute request are interleaved with the code cell execute request.... etc.. |
Wouldn't it be nice to have a variant of this that updated automatically by either querying the value from the kernel whenever a cell is run or getting the new value pushed from the whenever it changes? I'd like that :) |
We've discussed that with @fperez at scipy, and we believe it is dangerous in the sens that you can change the value of e variable
would be rendered as
But yes, it is doable. I'm thinking of embeding also the prompt number so that you could say:
|
Isn't the prompt number already available as a special variable? |
Ohh, this is very nice. A few questions then:
On Wed, Nov 28, 2012 at 1:46 AM, Benedikt Sauer notifications@github.comwrote:
Brian E. Granger |
Yes, it in in metadata... bu if you have a real
I'm open to suggestion. ::something:: was easy to parse and sufficiently different from md/html/code, fast to type.
Actually at convert time you can render the html with phantom js as png and put it as an image in TeX... where is the problem ?
I think I prefer my approach : extract from ipynb diff + "cell_type": "markdown",
+ "metadata": {
+ "literate": {
+ "data": {
+ "a": "set([1, 2])",
+ "f": "<function <lambda> at 0x102383758>",
+ "hello": "'world'",
+ "iDontExist": "[ERROR] KeyError: u'iDontExist'",
+ "x": "16"
+ }, Don't you like it ? No kidding, I think it would be great to extend to have 'variableName':{
'text' : 'whatever',
'LaTeX' : '\w{ha}t_ever',
'png' : '24e5bcf6.....4'
} |
To argue against me, |
How about extending the link syntax, in a similar way as it is for images, i.e. something like
Or, to use it in LaTeX:
Looks a bit clunky, but it would be quite flexible this way and I don't think that a prefix @ is used yet. |
Does not merge cleanly anymore. |
Other alternative I though of where things Jinja Style |
This allow to add a name of variable in MD cell between colon like ::x:: and it will be replace by variable value after cell rendering. value are store in metadata to stay between notebook save/reload
Rebased and update to support The only gotcha so far is that you can't actually write a string in a n-backtick environement in a markdown cell that would display as |
Any discussion feedback on the syntax we should use ? |
I will do a full review of this soon... On Sun, Jan 20, 2013 at 11:00 AM, Bussonnier Matthias <
Brian E. Granger |
Great ! Thanks. |
This is really important, and we need to think extremely carefully about how we expose this without mucking with markdown / mathjax. I don't know if this is feasible, but I would actually love it if this could be implemented as a js plugin. Even if we don't go that way, understanding what would be involved in a third party developing something like this will help us understand what kind of capabilities the jsplugin arch needs to have. |
I'm not sure this is totally doable as a js plugin as you indeed interfere with the way markdown is rendered.
What could be done is the ability to insert a call to a function between 'render request' call and 'do rendering' The intermediate function would be responsible for calling 'do rendering' with the modified source. The problem here is that if the kernel is busy, you wait for the cell to be rendered, and if you go the callback way it is difficult to chain the 'modifier' functions. |
I don't mean to say that we must do it this way, I just think we should think it through, so we have it as a case study for either A. something that our jsplugin arch should be able to handle, or
For instance, I think this would be an indicator that it has not been done correctly. The replacements should be async, and probably applied on the rendered HTML, rather than the markdown source forcing a re-render of the whole cell. So the steps would be:
|
It also suggests that our markdown render should be implemented as a series of prerender hooks (on markdown) and postrender hooks (on html), so that the transforms are more readily extensible. Current master has just the one of each - remove_math in prerender and replace_math in postrender. |
Just FYI, I've also been working a way to have live updates to a frontend as variables change. I've been working in the framework of interactive controls (see this example), but this could easily also work with rendered markdown things too. |
Right now they are asyn, with placeholder while it waits.
You might not want that, as this would mean to 'break' the current structure of the HTML or guess the classes it would have been wrapped in when converted by markdown converter. It is possible to do that with mathjax as you know this is a full 'block'. And you will probably not be able to replace variable in math expression either like that. Maybe you also want to restrict those variable to be purely text, in which case we can wrap them in a named span, (but not for mathjax) , but in the end, I would like to ask in the format to have a specific representation of a variable, which I'm not sure will play nicely with it.
We agree from 1 to 3. So do we intend to :
How will it play with converters like nbconvert ? The last question is how do you make the API ? Do you totally leave the post receive hook write the HTML ? Do you provide a 'placeholder/replacement value' interface ? How do you chain them ? Either we impose those transformer to be synchronous
Or fully async, we shoudl be able to do :
and By looping through all registered callback in reverse order build a nested loop of callback that in the end will call the native render. |
Async chaining of methods... with a not so obvious closure.
Prints
|
Your .docs/examples Notebook needs to be moved to /examples. Also, please make sure the style of the sample notebook matches the new formatting and naming of the example notebooks. |
OK I finally got to playing with this. Some first impressions:
I haven't done a line-by-line review of the code, but here are some broader comments:
|
One other comment, I think we should format the KeyError stuff slightly differently. To help set the error text apart from surrounding text, something like: |
We can see later, let's focus on pure text then.
Ok, I'll look into that.
Protocol should not return an error as string. |
This PR has been sitting for over a month now. This functionality is really important, but we want to get it right. Seems like you were going to work further on the code, in particular how the various stages of markdown processing are handled. Should we leave this open for you to continue that in place? Open an issue? |
I don't know how viable this is--you may end up having to do a bit of work to get it--but have you ever considered using an actual grammer and parser for Markdown? Then, extensions like this would be as simple as extending the grammar file, and you could be much more confident that the parser is still correct. |
Right now, I'm overwhelmed. As the right way to do it with parser or else, I have no idea. |
OK here is what I think we should do: I think the design issues/questions are big enough that we really want to think through things before we take this any further. When things are a bit more calm, we should have a Google+ or HipChat session to work out the rest of the details. I am going to close this and open an issue to track the broader discussion. @Carreau you know that we are super excited about this work - please don't take this as a vote against this idea. We will get these things figured out so you can move forward with this - also, when you have less on your plate. Thanks for you hard work! |
Closing, discussion will continue in #2958. |
Ability to show value of kernel variable in MD cell.
Do not view example with nbviewer, it does not support it yet.
allow to write code like
then in MD cell
a=::a::
which will be rendered asa=1
This does not allow repr_tex, or anything else as it uses
user_variable
.Should we consider extending protocol ? Did I missed a clean way to do it ?