Replies: 5 comments 1 reply
-
I have to say, I'm not100% up to speed on this. But I think I get where you're going. I don't use markdown much and haven't used those other packages you mentioned either, so I don't have any experience with this. Does the actual HTML, which is what plots and such are, not render in these environments? I just googled 'html in markdown', since I'm not that familiar with it and it seems to imply inline html works, and would be rendered, in markdown w/out trouble; so it says :) . I'm not sure how to try that. Your comment about
Make me wonder if you were running w/ batch mode False in the case you were trying(https://sassoftware.github.io/saspy/advanced-topics.html#using-batch-mode). If you're running somewhere other than where Ipython Dispaly will work, then setting Batch to True will return you the actual HTML document for the plot, chart, graph ... instead of trying to call the Display method on it. In your pdf document, the heatmap method (in Batch mode) would simply return the html document itself, and then, would that have rendered? |
Beta Was this translation helpful? Give feedback.
-
I've been trying to research this to be able to have an intelligent conversion. I've been playing with markdown cells in jupyter, as a means to see what works and what doesn't. Again, I don't have any experience with what you're trying to do here. I do see that html doesn't render in markdown. saspy output (plots, graphs, ...) are HTML5 with the images embedded; that's key to how it works in the various notebook server it supports. Getting the raw html, instead of some display object, is easy enough (Batch=True), and even extracting the "<img .../>" out of it is simple enough, yet still doesn't render via markup (that I can get to work): I tried the So, I'm trying to figure out what you're looking for from saspy, in regards to this. I assume there's no Python packages, like all of the R things you've mentioned, that can do anything to help take html and convert it to anything markdown can support? I don't do anything like this, so I don't know if there are or aren't. Did you say you have something that works for you? What is that, if so? FWIW, I did find a bit of a kludgy way to get this to be able to work. It's not a solution, but perhaps more of a brainstorming that may lead to something. I have an option in the config file, where you can change the output from HTML5 to HTML. I added that for someone trying to use SAS 9.3 (doesn't have html5) on their pc with local SAS. For that case, using HTML (images aren't embedded) happens to work, since the image files written out are linked in the html, and since they are all on the same machine, they render. If this were a remote connection to SAS, the image files wouldn't be found, nor displayed on the client. But, with HTML, the images are written out a .png files, on the SAS server side (current dir is what I observed), and the name of the file is in the html returned. SASPy has a download method, so I just extracted the filename from the html and downloaded the .png file, and then using the I know that's a bit of a stretch, and it's not a solution, but maybe there's an idea in there that's part of something. Again, I'm just trying to even figure out the problem, as much as what you're looking for to solve it. Thoughts? |
Beta Was this translation helpful? Give feedback.
-
Thank you for your effort in this @tomweber-sas , I'll try to be as helpful as possible. Markdown image inclusionI think that the image snippet didn't work for you because I truncated it, due to the size. I'm attaching the Including a file is also supported, and it's very often used: matplotlib and R code chunks generally use that approach, they save the file to a folder and add the appropriate code for including it (check the md file, the only plot which will be visible is the SAS one since the other files references are in my filesystem). HTML to markdownYou mentioned
Well, that's exactly the change that I did, so yes, you're right: with batch mode the logic can be done in Python by using BeautifulSoup and markdownify, as long as we can get access to the HTML (so, from bs4 import BeautifulSoup as BS
from markdownify import markdownify as md
def mdHTML(x):
soup = BS(x,features="lxml")
body = soup.find('body')
return(md(str(body))) The difference being that this change at the SASpy level makes it transparent to use, whereas without it the logic must be repeated in each block (even if it's only a single function). Now that I think of it perhaps this is good enough and not really worth adding more complexity at the SASpy layer, even if it would - from an end-user PoV - be better... after some decades we're back to the Worse is Better debate :) More than a change, maybe this is more worth a blog post so that those that have a similar need can learn how to do it (and hopefully this discussion will become a reference in itself). SASpy HTML and HTML5I didn't saw that one, but yes, if running the code saves a file with a predictable name then it would be possible to work with that. The challenged would be the need to drop the HTML output and replace it for the Markdown equivalent, which ends up being similar to the previous approach (with an URI insted of Having a more general mechanism that essentially returns values (which could be the PNG image data or the PNG filename in the local filesystem) could be a way to address all needs without requiring parsing the HTML in the code. It would also work regardless of the markup language: Rst, Asciidoc, Markdown, all have slightly different formats, but as long as one can get the image data or path and the name of the plot, one can get it working with minimal effort. In short...Whether this is useful enough for inclusion I don't know... it's useful to me and I'm using it successfully. I can see three main options:
Maybe the first option is good enough, and considering one can define a function in the initial code chunk the impact is very small: the need to add the display logic is still left to the end user but it's simplified enough that I don't think it's an issue. |
Beta Was this translation helpful? Give feedback.
-
Ok, there a lot of stuff there. Let me try to hit things as I go. For the Seeing you’re source file, I see you’re just using the "```python" wrapper around the python code, which doesn’t end up rendering the results be they Ipython objects or HTML strings. I couldn’t get any variation of the html to work with that either, whether it be the whole document, of just the img part of it, even when wrapping the <img with The HTML instead of HTML5, isn’t a real solution, but rather an idea if you’re just trying to put together a one off kind of thing to just accomplish building a doc. Not a real solution. Trying to turn that in to a generalized use case isn’t really an answer. To speak to your ‘In Short’ list:
You mentioned defining your function up front so it’s there to be used, you can do things like that in the config file, as it’s a Python file that’s imported, so functions and things you code in it are available as soon as you import saspy. But, again, I think that speaks to number 3, where defining dedicated transform methods would be a perfectly reasonable, and useful, contribution. Then you have the methods to make the native HTML that’s already provided, turned into whatever it is you need for the scripting language you’re trying to use (markdown or whatever). That seems the most straightforward thing to me so far. I hope that addresses your thoughts and even makes some sense. I'm thinking that exploring number 3 above, one way or another, is probably then next thing to do; in my mind anyway. What are your thoughts? Thanks! |
Beta Was this translation helpful? Give feedback.
-
I think we're on to something :) Trying to be as concise as possible to avoid drowning you with details: The SGPlot thing: it won't render in Jupyter, only in Markdown. That is exactly the change needed for it to work in Markdown, the move from HTML (which Jupyter renders) to Markdown (which gets correctly parsed by Markdown interpreters but will not be rendered in Jupyter). This is indeed the crux of the matter: when used in a Markdown document the results needs to be in markdown. The md file is the result of parsing the Rmd with
When using Codebraid (a different system which isn't based on R but performs a similar function - it parses a .cmd file and produces a md one after running the code and merging the output) I can actually specify the jupyter kernel that will run the code, but this doesn't change the end result: HTML or iPython output will not work since Markdown processors won't be able to transform them into appropriate content (like going from an HTML table to a PDF table, or an HTML image to a png, etc). Now, the reason you didn't saw anything in the python code is because (and perhaps I should've been clearer in this) I changed SASpy to add the necessary methods: this goes directly to the discussion on what option to follow. My local copy of SASpy has that So, you essentially described my current working approach (again, apologies, I should've been clearer but when one's knee-deep in code it's easy to forget that context is very important). On my side I can do two things right now:
Point 2 above is without any expectation of merging, but merely to show what I've done so that any next step on this can have an additional reference point. |
Beta Was this translation helpful? Give feedback.
-
After having great success in using SASpy in Jupyter notebooks I started to wonder if I could use it in Markdown documents; the reasons are plenty and include the inability to produce configurable PDFs from Jupyterlab since there are no images, plus the different use-case I have (producing documentation that is easy to edit and maintain through git, etc)
One of the reasons this isn't directly possibly is because an
<IPython.core.display.HTML object>
will be present in non-browser settings.I don't know enough (or even the basics) of ODS, so I suspect my approach is not the most direct, but I added a new "display" called "markdown" that uses BeatifulSoup + markdownify to output Markdown markup - very simple one since it wraps the
img
element.With this change I could use SASpy to produce plots in the following systems:
Rmd
file with the python engine (which essentially means that the same Rmd file can actually have SAS, R and Python code in it)While R has a SAS engine, using SASpy is, I think, the only way to use a remote SAS Viya instance, and this was the configuration I tested.
I think this could be worth exploring (and the changes needed are minimal and I can share them, albeit I haven't tested it thoroughly).
I'm attaching an example PDF for reference, built from an Rmd file - merely an example.
SAS.pdf
Beta Was this translation helpful? Give feedback.
All reactions