Jump to content

[SOLVED] Workflow to render GitHub ipynb files nicely using nbviewer.


Recommended Posts

Update:  The MWE workflow can be found here.

 

 

tldr; Needed help using appplescrip to edit string, I know how to do in python.  (The required workflow is working help needed to make it better.)

 

I was writing an Alfred Workflow to render ipython notebooks `.ipynb` files nicely using nbviewer of github.

For that we need to use the website: https://nbviewer.jupyter.org

 

For example to render a ipynb file:

 https://github.com/bhishanpdl/pdl_practice/blob/master/a.ipynb

 

We need to open https://nbviewer.jupyter.org and paste the above notebook url there.

 

i.e.

From: https://github.com/bhishanpdl/pdl_practice/blob/master/a.ipynb

To:  https://nbviewer.jupyter.org/github/bhishanpdl/pdl_practice/blob/master/a.ipynb

 

Then, we just need to open the new url in safari.

----------

I did following:

- applescrip to find frontmost tab url and copy to clipboard

-  edit the url using python

- again use applescript to open edited url

 

This seems to be bit of too much of a work.

 

Is there way it can be done in only one applescript. The only thing I don't know is how to edit the string in applescript. 

I am only comfortable working with python and use it for most of the work.

 

nbviewer.png.7e7c4f6a1a5b2480ce575d56fda9bfaa.png

 

 

 

The codes used are following:

 

 

 

# First applescript
tell application "Safari"
	ignoring case
		set theURL to URL of front document
		set the clipboard to theURL
	end ignoring
end tell


# Second python script to edit url
import sys
query = sys.argv[1]
query = 'https://nbviewer.jupyter.org/github'+ query.split(r'github.com')[1]
sys.stdout.write(query)

"""
From:                       https://github.com/bhishanpdl/pdl_practice/blob/master/a.ipynb
To  : https://nbviewer.jupyter.org/github/bhishanpdl/pdl_practice/blob/master/a.ipynb
"""


# Last applescript to open url
on alfred_script(q)
  set theURL to the clipboard
  tell application "Safari"
    open location theURL
    activate
end tell
end alfred_script

 

How can all these codes can be written in only one applescript?

Edited by Bhishan
Link to post
On 9/17/2018 at 12:23 AM, Bhishan said:

tldr; Needed help using appplescrip to edit string

 

On 9/17/2018 at 12:23 AM, Bhishan said:

How can all these codes can be written in only one applescript?

 

You keep asking about the technology instead of the solution. That makes you susceptible to the XY problem. Unless you really need the specific technology (which hasn’t been the case), concentrate on explaining the issue and desired outcome clearly, and let the helper suggest the language.


For example, in this case AppleScript isn’t necessary at all to open the webpage. A regular system call to open with a URL would suffice. Or in your example, even Alfred’s native Open URL Action.

 

On 9/17/2018 at 12:23 AM, Bhishan said:

on alfred_script(q)

 

Don’t use Run NSAppleScript unless you know you need it. You’re not doing anything with q, so you don’t. Use a Run Script with osascript as the language instead.


Also, please don’t forget to upload your Workflow when asking for help, so we have something to work from. I know you usually do it, so this is simply a reminder that it’s always useful.


Finally, here’s a solution with JXA. It works with multiple browsers, as it’s based on my gist.

Edited by vitor
Link to post

@vitor   Apologies for forgetting to upload the  MWE workflow.

 

  For completeness,  uploaded posthumously:  workflow link

 

Its sunday evening here in Ohio USA. I was not expecting answer until monday.  The Community of Alfred is awesome!

 

I know there is one father and one mother of Alfred, I guess there are so many brother and sisters of Alfred who are helpful and active 

almost all the time.

 

Sorry for the probable XY question. Rather, I should have explained the problem and seek for the suggestions from the expert.

 

 

Its always tempting to beliveve that if we know the small part, we can ourself solve the rest of the problem and avoid taking too much time of the helpers. But sometimes it goes the wrong way and instead the asker needs too much time of helper.

 

Thanks again, the new workflow with JXA works nice and also helps to write further similar workflow.

I need to look and grab some basic concepts of JXA now.

 

 

Link to post

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...