GitHub PullRequestEvent at 2014-11-11 03:37:00
Repository: ipython
Actor: minrk
Payload: {"action":"opened","number":6896,"pull_request":{"url":"https://api.github.com/repos/ipython/ipython/pulls/6896","id":24196555,"html_url":"https://github.com/ipython/ipython/pull/6896","diff_url":"https://github.com/ipython/ipython/pull/6896.diff","patch_url":"https://github.com/ipython/ipython/pull/6896.patch","issue_url":"https://api.github.com/repos/ipython/ipython/issues/6896","number":6896,"state":"open","locked":false,"title":"Proposal: add pre/post-save hooks","user":{"login":"minrk","id":151929,"avatar_url":"https://avatars.githubusercontent.com/u/151929?v=3","gravatar_id":"","url":"https://api.github.com/users/minrk","html_url":"https://github.com/minrk","followers_url":"https://api.github.com/users/minrk/followers","following_url":"https://api.github.com/users/minrk/following{/other_user}","gists_url":"https://api.github.com/users/minrk/gists{/gist_id}","starred_url":"https://api.github.com/users/minrk/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/minrk/subscriptions","organizations_url":"https://api.github.com/users/minrk/orgs","repos_url":"https://api.github.com/users/minrk/repos","events_url":"https://api.github.com/users/minrk/events{/privacy}","received_events_url":"https://api.github.com/users/minrk/received_events","type":"User","site_admin":false},"body":"We may not want to do this so close to 3.0, but it's a mitigation of the removed `--script` flag, so I thought it was worth discussing. Hooks are Python functions that are passed the model, path, and content_manager. I started with two hooks:\n\n- `ContentsManager.pre_save_hook` runs on the path and model with content. This one can be used for things like stripping output that people don't like adding to VCS noise.\n- `FileContentsManager.post_save_hook` runs on the filesystem path and model without content. This one can be used to replace `--script` with a call to nbconvert.\n\nAn example `ipython_notebook_config.py` that does both of these things:\n\nA pre-save hook for stripping output:\n\n```python\n\ndef scrub_output_pre_save(model, **kwargs):\n    \"\"\"scrub output before saving notebooks\"\"\"\n    # only run on notebooks\n    if model['type'] != 'notebook':\n        return\n    # only run on nbformat v4\n    if model['content']['nbformat'] != 4:\n        return\n    \n    model['content']['metadata'].pop('signature', None)\n    for cell in model['content']['cells']:\n        if cell['cell_type'] != 'code':\n            continue\n        cell['outputs'] = []\n        cell['execution_count'] = None\n\nc.FileContentsManager.pre_save_hook = scrub_output_pre_save\n```\n\nA post-save hook to replace `--script` (this could be a simple call to `ipython nbconvert --to notebook`, but spawning the subprocess every time is quite slow):\n\n```python\nimport io\nimport os\nfrom IPython.nbconvert.exporters.python import PythonExporter\n\nexporter = None\n\ndef script_post_save(model, os_path, contents_manager, **kwargs):\n    \"\"\"convert notebooks to Python script after save with nbconvert\n    \n    replaces `ipython notebook --script`\n    \"\"\"\n    log = contents_manager.log\n    global exporter\n    if model['type'] != 'notebook':\n        return\n    if exporter is None:\n        exporter = PythonExporter(config=contents_manager.config)\n    \n    base, ext = os.path.splitext(os_path)\n    py_fname = base + '.py'\n    log.info(\"Writing %s\", py_fname)\n    py, resources = exporter.from_filename(os_path)\n    with io.open(py_fname, 'w', encoding='utf-8') as f:\n        f.write(py)\n\nc.FileContentsManager.post_save_hook = script_post_save\n```","created_at":"2014-11-11T03:37:00Z","updated_at":"2014-11-11T03:37:00Z","closed_at":null,"merged_at":null,"merge_commit_sha":null,"assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/ipython/ipython/pulls/6896/commits","review_comments_url":"https://api.github.com/repos/ipython/ipython/pulls/6896/comments","review_comment_url":"https://api.github.com/repos/ipython/ipython/pulls/comments/{number}","comments_url":"https://api.github.com/repos/ipython/ipython/issues/6896/comments","statuses_url":"https://api.github.com/repos/ipython/ipython/statuses/f1fa0bdc81829c12f2d9849d5049a72ca6f3ed97","head":{"label":"minrk:save-hooks","ref":"save-hooks","sha":"f1fa0bdc81829c12f2d9849d5049a72ca6f3ed97","user":{"login":"minrk","id":151929,"avatar_url":"https://avatars.githubusercontent.com/u/151929?v=3","gravatar_id":"","url":"https://api.github.com/users/minrk","html_url":"https://github.com/minrk","followers_url":"https://api.github.com/users/minrk/followers","following_url":"https://api.github.com/users/minrk/following{/other_user}","gists_url":"https://api.github.com/users/minrk/gists{/gist_id}","starred_url":"https://api.github.com/users/minrk/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/minrk/subscriptions","organizations_url":"https://api.github.com/users/minrk/orgs","repos_url":"https://api.github.com/users/minrk/repos","events_url":"https://api.github.com/users/minrk/events{/privacy}","received_events_url":"https://api.github.com/users/minrk/received_events","type":"User","site_admin":false},"repo":{"id":659446,"name":"ipython","full_name":"minrk/ipython","owner":{"login":"minrk","id":151929,"avatar_url":"https://avatars.githubusercontent.com/u/151929?v=3","gravatar_id":"","url":"https://api.github.com/users/minrk","html_url":"https://github.com/minrk","followers_url":"https://api.github.com/users/minrk/followers","following_url":"https://api.github.com/users/minrk/following{/other_user}","gists_url":"https://api.github.com/users/minrk/gists{/gist_id}","starred_url":"https://api.github.com/users/minrk/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/minrk/subscriptions","organizations_url":"https://api.github.com/users/minrk/orgs","repos_url":"https://api.github.com/users/minrk/repos","events_url":"https://api.github.com/users/minrk/events{/privacy}","received_events_url":"https://api.github.com/users/minrk/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/minrk/ipython","description":"","fork":true,"url":"https://api.github.com/repos/minrk/ipython","forks_url":"https://api.github.com/repos/minrk/ipython/forks","keys_url":"https://api.github.com/repos/minrk/ipython/keys{/key_id}","collaborators_url":"https://api.github.com/repos/minrk/ipython/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/minrk/ipython/teams","hooks_url":"https://api.github.com/repos/minrk/ipython/hooks","issue_events_url":"https://api.github.com/repos/minrk/ipython/issues/events{/number}","events_url":"https://api.github.com/repos/minrk/ipython/events","assignees_url":"https://api.github.com/repos/minrk/ipython/assignees{/user}","branches_url":"https://api.github.com/repos/minrk/ipython/branches{/branch}","tags_url":"https://api.github.com/repos/minrk/ipython/tags","blobs_url":"https://api.github.com/repos/minrk/ipython/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/minrk/ipython/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/minrk/ipython/git/refs{/sha}","trees_url":"https://api.github.com/repos/minrk/ipython/git/trees{/sha}","statuses_url":"https://api.github.com/repos/minrk/ipython/statuses/{sha}","languages_url":"https://api.github.com/repos/minrk/ipython/languages","stargazers_url":"https://api.github.com/repos/minrk/ipython/stargazers","contributors_url":"https://api.github.com/repos/minrk/ipython/contributors","subscribers_url":"https://api.github.com/repos/minrk/ipython/subscribers","subscription_url":"https://api.github.com/repos/minrk/ipython/subscription","commits_url":"https://api.github.com/repos/minrk/ipython/commits{/sha}","git_commits_url":"https://api.github.com/repos/minrk/ipython/git/commits{/sha}","comments_url":"https://api.github.com/repos/minrk/ipython/comments{/number}","issue_comment_url":"https://api.github.com/repos/minrk/ipython/issues/comments/{number}","contents_url":"https://api.github.com/repos/minrk/ipython/contents/{+path}","compare_url":"https://api.github.com/repos/minrk/ipython/compare/{base}...{head}","merges_url":"https://api.github.com/repos/minrk/ipython/merges","archive_url":"https://api.github.com/repos/minrk/ipython/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/minrk/ipython/downloads","issues_url":"https://api.github.com/repos/minrk/ipython/issues{/number}","pulls_url":"https://api.github.com/repos/minrk/ipython/pulls{/number}","milestones_url":"https://api.github.com/repos/minrk/ipython/milestones{/number}","notifications_url":"https://api.github.com/repos/minrk/ipython/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/minrk/ipython/labels{/name}","releases_url":"https://api.github.com/repos/minrk/ipython/releases{/id}","created_at":"2010-05-10T18:37:01Z","updated_at":"2014-08-30T01:59:05Z","pushed_at":"2014-11-11T03:31:25Z","git_url":"git://github.com/minrk/ipython.git","ssh_url":"git@github.com:minrk/ipython.git","clone_url":"https://github.com/minrk/ipython.git","svn_url":"https://github.com/minrk/ipython","homepage":"","size":77820,"stargazers_count":16,"watchers_count":16,"language":"Python","has_issues":false,"has_downloads":true,"has_wiki":false,"has_pages":false,"forks_count":1,"mirror_url":null,"open_issues_count":0,"forks":1,"open_issues":0,"watchers":16,"default_branch":"master"}},"base":{"label":"ipython:master","ref":"master","sha":"39315baa54711bda666ed891c73422deb7f8301a","user":{"login":"ipython","id":230453,"avatar_url":"https://avatars.githubusercontent.com/u/230453?v=3","gravatar_id":"","url":"https://api.github.com/users/ipython","html_url":"https://github.com/ipython","followers_url":"https://api.github.com/users/ipython/followers","following_url":"https://api.github.com/users/ipython/following{/other_user}","gists_url":"https://api.github.com/users/ipython/gists{/gist_id}","starred_url":"https://api.github.com/users/ipython/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ipython/subscriptions","organizations_url":"https://api.github.com/users/ipython/orgs","repos_url":"https://api.github.com/users/ipython/repos","events_url":"https://api.github.com/users/ipython/events{/privacy}","received_events_url":"https://api.github.com/users/ipython/received_events","type":"Organization","site_admin":false},"repo":{"id":658518,"name":"ipython","full_name":"ipython/ipython","owner":{"login":"ipython","id":230453,"avatar_url":"https://avatars.githubusercontent.com/u/230453?v=3","gravatar_id":"","url":"https://api.github.com/users/ipython","html_url":"https://github.com/ipython","followers_url":"https://api.github.com/users/ipython/followers","following_url":"https://api.github.com/users/ipython/following{/other_user}","gists_url":"https://api.github.com/users/ipython/gists{/gist_id}","starred_url":"https://api.github.com/users/ipython/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ipython/subscriptions","organizations_url":"https://api.github.com/users/ipython/orgs","repos_url":"https://api.github.com/users/ipython/repos","events_url":"https://api.github.com/users/ipython/events{/privacy}","received_events_url":"https://api.github.com/users/ipython/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/ipython/ipython","description":"Official repository for IPython itself. Other repos in the IPython organization contain things like the website, documentation builds, etc.","fork":false,"url":"https://api.github.com/repos/ipython/ipython","forks_url":"https://api.github.com/repos/ipython/ipython/forks","keys_url":"https://api.github.com/repos/ipython/ipython/keys{/key_id}","collaborators_url":"https://api.github.com/repos/ipython/ipython/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/ipython/ipython/teams","hooks_url":"https://api.github.com/repos/ipython/ipython/hooks","issue_events_url":"https://api.github.com/repos/ipython/ipython/issues/events{/number}","events_url":"https://api.github.com/repos/ipython/ipython/events","assignees_url":"https://api.github.com/repos/ipython/ipython/assignees{/user}","branches_url":"https://api.github.com/repos/ipython/ipython/branches{/branch}","tags_url":"https://api.github.com/repos/ipython/ipython/tags","blobs_url":"https://api.github.com/repos/ipython/ipython/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/ipython/ipython/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/ipython/ipython/git/refs{/sha}","trees_url":"https://api.github.com/repos/ipython/ipython/git/trees{/sha}","statuses_url":"https://api.github.com/repos/ipython/ipython/statuses/{sha}","languages_url":"https://api.github.com/repos/ipython/ipython/languages","stargazers_url":"https://api.github.com/repos/ipython/ipython/stargazers","contributors_url":"https://api.github.com/repos/ipython/ipython/contributors","subscribers_url":"https://api.github.com/repos/ipython/ipython/subscribers","subscription_url":"https://api.github.com/repos/ipython/ipython/subscription","commits_url":"https://api.github.com/repos/ipython/ipython/commits{/sha}","git_commits_url":"https://api.github.com/repos/ipython/ipython/git/commits{/sha}","comments_url":"https://api.github.com/repos/ipython/ipython/comments{/number}","issue_comment_url":"https://api.github.com/repos/ipython/ipython/issues/comments/{number}","contents_url":"https://api.github.com/repos/ipython/ipython/contents/{+path}","compare_url":"https://api.github.com/repos/ipython/ipython/compare/{base}...{head}","merges_url":"https://api.github.com/repos/ipython/ipython/merges","archive_url":"https://api.github.com/repos/ipython/ipython/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/ipython/ipython/downloads","issues_url":"https://api.github.com/repos/ipython/ipython/issues{/number}","pulls_url":"https://api.github.com/repos/ipython/ipython/pulls{/number}","milestones_url":"https://api.github.com/repos/ipython/ipython/milestones{/number}","notifications_url":"https://api.github.com/repos/ipython/ipython/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/ipython/ipython/labels{/name}","releases_url":"https://api.github.com/repos/ipython/ipython/releases{/id}","created_at":"2010-05-10T04:46:06Z","updated_at":"2014-11-10T22:44:31Z","pushed_at":"2014-11-11T02:33:41Z","git_url":"git://github.com/ipython/ipython.git","ssh_url":"git@github.com:ipython/ipython.git","clone_url":"https://github.com/ipython/ipython.git","svn_url":"https://github.com/ipython/ipython","homepage":"http://ipython.org","size":128437,"stargazers_count":5152,"watchers_count":5152,"language":"Python","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":1506,"mirror_url":null,"open_issues_count":901,"forks":1506,"open_issues":901,"watchers":5152,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/ipython/ipython/pulls/6896"},"html":{"href":"https://github.com/ipython/ipython/pull/6896"},"issue":{"href":"https://api.github.com/repos/ipython/ipython/issues/6896"},"comments":{"href":"https://api.github.com/repos/ipython/ipython/issues/6896/comments"},"review_comments":{"href":"https://api.github.com/repos/ipython/ipython/pulls/6896/comments"},"review_comment":{"href":"https://api.github.com/repos/ipython/ipython/pulls/comments/{number}"},"commits":{"href":"https://api.github.com/repos/ipython/ipython/pulls/6896/commits"},"statuses":{"href":"https://api.github.com/repos/ipython/ipython/statuses/f1fa0bdc81829c12f2d9849d5049a72ca6f3ed97"}},"merged":false,"mergeable":null,"mergeable_state":"unknown","merged_by":null,"comments":0,"review_comments":0,"commits":1,"additions":88,"deletions":7,"changed_files":2}}