twibiral / obsidian-execute-code

Obsidian Plugin to execute code in a note.
MIT License
1.09k stars 66 forks source link

[BUG(ish)] Magic command flaw. #338

Closed wallysaurus closed 5 months ago

wallysaurus commented 5 months ago

So I have this fun little code snippet:

current = spotify.currently_playing()
album_url = current['item']['album']['images'][0]['url']

@show(album_url)

where I'm grabbing an image link from spotify's api based on what song I'm currently listening to. was losing my mind over trying to make matplotlib handle just raw images rather than trying to display them in complex diagrams. saw that this plugin supported "magic commands" which could essentially just let me plug the link straight in as a single argument BEAUTY AT LAST! except these magic commands are coded to only accept raw text, and not variables, since they're parsed before compile. womp womp back to mathplotlib again i guess..

also for some reason your mathplotlib images get displayed with these ugly horizontal whitespace bars on each side of the image. i tested the python code locally and couldn't replicate this problem, so i'm guessing it's how you handle it on your side.

twibiral commented 5 months ago

Hi, the magic commands are resolved before running the code, otherwise we would have to implement each magic command separately for each programming language (like we already do for the plot command). That would be annoying. I suggests you simply use python to grab the image from the url and save it to a fixed path, then you can use the magic command to display the image:

img_data = requests.get(image_url).content
with open('path/to/image.jpg', 'wb') as handler:
    handler.write(img_data)

@show("path/to/image.jpg")

Concerning the white bars with matplotlib: I never saw this in my own notes. Maybe its because of your version of matplotlib or python, or because of a theme.