Package: ur
urimport remote Python and Jupyter notebook files, from GitHub Gists, the local filesystem, or arbitrary URLs.Install:fromsysimportexecutableaspython!{python}-mpipinstall-qurUsageImport GitHub gistsfromgists._1288bff2f9e05394a94312010da267bbimport*a_b.a(),a_b.b(),c.c()('aaa', 'bbb', 'ccc')That imports 2 Jupyter notebooks fromhttps://gist.github.com/1288bff2f9e05394a94312010da267bb(note the leading underscore in theimportstatement, which is necessary when the Gist ID begins with a number), and calls functions defined in them.Import arbitrary URLsTheurmodule exposes a powerful API for importing code from {local,remote} {.py,.ipynb} files.Here is an example directly importing one of the files inthe example gist used above:importura_b=ur('https://gist.githubusercontent.com/ryan-williams/1288bff2f9e05394a94312010da267bb/raw/a_b.ipynb')a_b.a(),a_b.b()('aaa', 'bbb')Import wildcardsIn addition to calling theurmodule (and returning a module), the*operator can be used:importurur*'https://gist.github.com/1288bff2f9e05394a94312010da267bb'a_b.a(),a_b.b(),c.c()('aaa', 'bbb', 'ccc')This is is analogous toimport *syntax, but can be used to import from arbitrary URLs (in this case,urdetects that the URL represents a gist, and imports the two.ipynbmodules found there).Here is an equivalent import using theur(…)syntax:importurur(gist='1288bff2f9e05394a94312010da267bb',all='*')a_b.a(),a_b.b(),c.c()('aaa', 'bbb', 'ccc')importururl='https://gist.githubusercontent.com/ryan-williams/1288bff2f9e05394a94312010da267bb/raw/0a2b5966c22c5461734063b78239262e39e4f363/c.ipynb'ur(url,all=True)c()'ccc'DiscussionJupyter notebooks provide a rich, literate programming experience that is preferable to conventional IDE-based Python environments in many ways.However, conventional wisdom is that reusing code in notebooks requires porting it to.pyfiles. This is tedious and often requires trashing some of what makes notebooks great in the first place (rich, inline documentation, easy reproducibility, etc.).Jupyter itself providessample code for importing code from (local) Jupyter notebooks(originally from 2014?), andseveralpackages,repositories,blog posts,andissueshave built on and published similar code.nbimporter(which this repo is a fork of) is perhaps most notable, allowing seamless reuse of Jupyter-resident utilities within single projects, butits author now recommends factoring code out to.pymodules.The Jupyter ecosystem increasingly shines for the ease with which it allows of publishing and sharing notebooks, and stands to gain a lot from easier remixing+reuse of the wealth of code and data being published in Jupyter notebooks every day. I believe there are straightforward answers tothe reproducibility and testability concerns raised innbimporter, and built theurpackage to bear that out (and solve immediate productivity and code-reuse needs in my day-to-day life).Remote importing: package-less publishingAn animating idea ofuris that publishing+reusing a useful helper function should be no harder than
Reuse of code in Jupyter notebooks should be made as easy as possible. Users shouldn't have to mangle their utility-code and then publish it to Pip repositories in order to avoid copy/pasting standard helpers in every notebook/project they work in.Importing code directly from remote Notebooks (or.pyfiles) allows frictionless code reuse beyond what Python/Jupyter users are offered today.GitHub Gists: "anyone with the link can view" git repositoriesurparticularly emphasizes using and importing fromGitHub Gists. Likegititself, Gists combine a few simple but powerful concepts orthogonally, forming a great platform for sharing and tracking code:Gists are the only service I'm aware of that allows "publishing" a Git repository to an opaque URL that can be easily shared, but is otherwise (cryptographically, I think?) private, not search-indexed, etc.GitLab snippetsare a comparable product, buta request for this feature is open at time of writingEach Gist is backed by a full Git repository, under the hoodGists can therefore track changes to many files over timeUsers can choose to view Gists' "latestHEAD" content (the default onhttps://gist.github.com), or specify frozen Git-SHA permalinks for guaranteed reproducibilityboth "modes" are supported via web browser, Git CLI, orGitHub APIMany CLIs and SDKs exist for interacting with Gists from different languages/environmentsI previously wrote agist-dirhelperthat uploads an entire directory as a Gist (also working around issues with binary data that the Gist API normally doesn't handle correctly)Use-case: portable, shareable "dotfiles"Something thaturmakes easy is boilerplate-free reuse of common imports and aliases across notebooks/projects/users.For example, "everyone" importsnumpy as np,pandas as pd,plotly as pl, etc. I have a few that I like in addition:from os import environ as env,from sys import python as executable, etc.uroffers several minimal-boilerplate ways to let you (and anyone you share your notebook with) use all the helpers you like, portably, without having to redeclare them or otherwise interfere with the environment you originally used them in:importurur(github='ryan-williams/dotfiles',tree='v1.0',file='dotfiles.ipynb',all='*')Many versions of this can be used, depending on your preferences, e.g.:fromgists.abcdef0123456789abcdef0123456789import*Future workpretty-print info about what's imported (in notebook environments)test/handle intra-gist importstest/handle pip dependencies in gist importssupport github / gitlab importsAPI for tagging/skipping cells in notebooks (visualizations, tests, etc.)Support Jupyter versions >4Minimize(+freeze!)dependenciesproper loggingself-host (put code in notebooks in gists, implement subsequent versions ofurusing earlier versions ofurto import from remote, package-less locations)