gpoore / codebraid

Live code in Pandoc Markdown
BSD 3-Clause "New" or "Revised" License
367 stars 13 forks source link

How to use the Julia jupyter kernel to generate plots in output #53

Closed 00krishna closed 2 years ago

00krishna commented 2 years ago

I was trying to understand how to generate plots in a codebraid file with julia codeblocks. Julia has its own jupyter kernel, usually called IJulia.

Would something like this work, or does codebraid not yet support Julia jupyter kernels? Also, usually Julia jupyter kernels are attached to a Julia virtual environment. I think codebraid designed to pick the Julia environment that is in the same directory as the pandoc markdown file--as per the changelog and the addition of the --project=@. flag. So would this set then allow me to call the local IJulia jupyter kernel from the local Julia environment--as opposed to the global environment, etc.

I tried the following code:


```{.julia .cb-nb jupyter_kernel=julia session=random example=true}
using Plots
x = 1:10; y = rand(10); # These are the plotting data
plot(x, y)

While there are no errors, the file does not show the actual plot. I also attached the original file and the pdf. julia_output.pdf julia-braid-test.md

gpoore commented 2 years ago

There are a couple of issues. IJulia defaults to SVG output, which Codebraid currently ignores for display purposes. I can probably add support for that to the next release, which will be within the next few days. For now, you can use default(fmt = :png) or something similar to switch to a supported output format.

The other issue is that Starting kernel event loops. can appear at the start of the document whenever code runs, if you are writing your document to stdout or using Codebraid Preview. If you just rebuild the document (so the cache is used), this goes away. It looks like IJulia writes messages to stdout during startup. I don't know if there's a way to disable that. Otherwise I suppose Codebraid might be able to modify stdout during startup to capture the message.

00krishna commented 2 years ago

Excellent. Okay, I set the default to :png and that seems to have worked. So that is really nice. Thanks for the tip on that.

I sometimes see that kernel event loops message when running codebraid. I just checked my test document and it did not seem to be a problem, but I will stay on the lookout for it. So the solution, as you say, is just to build the file twice--so that the cache is used. I will keep that in mind. I am hoping to use codebraid to take the pandoc markdown and process it for running in a Hugo blog. So I suppose I just have to instruct my build routine to always run all codebraid build twice--to make sure those extra comments are not in the final output.

gpoore commented 2 years ago

The dev version on GitHub now fixes a bug in creating SVG files and adds SVG to the formats that are automatically displayed. So SVG will work automatically by default now. It's working very nicely in Codebraid Preview. A new release with these features will be on PyPI in the next day or so.

For the case of creating PDF files, you may still want PNG, depending on how you are telling Pandoc to create the PDF and whether that method supports SVG images.

gpoore commented 2 years ago

The kernel event loops message issue should now be solved as well, so no extra steps or building twice should be needed in the future for Jupyter kernels that write messages to stdout or stderr.