I am setting up a Hugo blog, and I wanted to write posts that include some
python code for generating plots. So the final HTML post should include both the original code and generated plots. Can anyone suggest the best way to do this?
I was looking at another post that talks about using
RMarkdown and the
reticulate library to run python code blocks. This could work, but it seems a bit odd to use an R library to run python code blocks. Is this the best way to run python code and generate code blocks now, or is there a native python approach to doing this?
Just was wondering what the best solution or approach was. Thanks.
The “best” is subjective .
I would choose to write the code blocks in Org mode and auto-generate the result code blocks (even plots) using Org Babel.
Here’s an example of how I generate the images using plantuml using Org + Org Babel, and the site’s published using Hugo of course.
My initial thought is to create a
Makefile to run your Python module before the Hugo build. Or run the Python module manually and when it’s complete, run your Hugo build.
Have it generate the plot then add the (image?) as a page resource to your post bundle with Python. Then possibly even have it write to your
index.md file to add the code snippet. Include logic in the corresponding Hugo template to check if an image with that filename exists as a page resource based on whatever criteria you want, and add it to an
<img/> in your HTML.
I think this would work, but I may be overcomplicating it.
@wjh18 Yeah, I hear you. I am thinking that there must be some preprocessor library that will run the markdown and generate the corresponding HTML. The example I gave is for using this RMarkdown library with
reticulate. So that will combination will run a markdown file through a preprocessor which will run the python code blocks and then insert the output into HTML.
There are other preprocessors like this for python, notably
Pweave, but I don’t think that is supported any more. I can use the RMarkdown method if that is the only way. I was just wondering if anyone had found a smoother toolchain.
Maybe Codebraid will fit your use-case.
@UtkarshVerma Oh codebraid does seem nice. I have not seen it before. Do you know if there is any integration between codebraid and Hugo? I imagine that I would need to write some sort of preprocessor that will run codebraid on the file before building it with hugo. Do you know if anyone has already done this. My google search turned up nothing.
Codebraid doesn’t have any Hugo integration. However, you can simply write scripts to preprocess the Markdown files before Hugo builds the site.
If you want live reload, then having Hugo watch the processed files, you can run Codebraid whenever the source file is updated through tools like entr.
Are you aware of PyScript?
@UtkarshVerma seems like
codebraid is the way to go. It does work for generating plots and all.
I will still have to manually process some of the markdown output to set it up for Hugo though, which means I have to write a script. But otherwise seems like it should work. Thanks for the tip.
This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.