1#!/usr/bin/env python 2# Copyright 2015-2017 ARM Limited 3# 4# Licensed under the Apache License, Version 2.0 (the "License"); 5# you may not use this file except in compliance with the License. 6# You may obtain a copy of the License at 7# 8# http://www.apache.org/licenses/LICENSE-2.0 9# 10# Unless required by applicable law or agreed to in writing, software 11# distributed under the License is distributed on an "AS IS" BASIS, 12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13# See the License for the specific language governing permissions and 14# limitations under the License. 15# 16 17 18"""This is a script to publish a notebook containing Ipython graphs 19The static data is published as an anonymous gist. GitHub does not 20allow easy deletions of anonymous gists. 21""" 22 23import os 24import argparse 25from IPython.nbformat.sign import TrustNotebookApp 26from argparse import RawTextHelpFormatter 27 28# Logging Configuration 29import logging 30from trappy.plotter import IPythonConf 31 32logging.basicConfig(level=logging.INFO) 33 34 35def change_resource_paths(txt): 36 """Change the resource paths from local to 37 Web URLs 38 """ 39 40 # Replace the path for d3-tip 41 txt = txt.replace( 42 IPythonConf.add_web_base("plotter_scripts/EventPlot/d3.tip.v0.6.3"), 43 IPythonConf.D3_TIP_URL) 44 txt = txt.replace( 45 IPythonConf.add_web_base("plotter_scripts/EventPlot/d3.v3.min"), 46 IPythonConf.D3_PLOTTER_URL) 47 txt = txt.replace( 48 IPythonConf.add_web_base("plotter_scripts/EventPlot/EventPlot"), 49 "https://rawgit.com/sinkap/7f89de3e558856b81f10/raw/46144f8f8c5da670c54f826f0c634762107afc66/EventPlot") 50 txt = txt.replace( 51 IPythonConf.add_web_base("plotter_scripts/ILinePlot/synchronizer"), 52 IPythonConf.DYGRAPH_SYNC_URL) 53 txt = txt.replace( 54 IPythonConf.add_web_base("plotter_scripts/ILinePlot/dygraph-combined"), 55 IPythonConf.DYGRAPH_COMBINED_URL) 56 txt = txt.replace( 57 IPythonConf.add_web_base("plotter_scripts/ILinePlot/ILinePlot"), 58 "https://rawgit.com/sinkap/648927dfd6985d4540a9/raw/69d6f1f9031ae3624c15707315ce04be1a9d1ac3/ILinePlot") 59 txt = txt.replace( 60 IPythonConf.add_web_base("plotter_scripts/ILinePlot/underscore-min"), 61 IPythonConf.UNDERSCORE_URL) 62 63 logging.info("Updated Library Paths...") 64 return txt 65 66 67def publish(source, target): 68 """Publish the notebook for globally viewable interactive 69 plots 70 """ 71 72 txt = "" 73 74 with open(source, 'r') as file_fh: 75 txt = change_resource_paths(file_fh.read()) 76 77 with open(target, 'w') as file_fh: 78 file_fh.write(txt) 79 80 trust = TrustNotebookApp() 81 trust.sign_notebook(target) 82 logging.info("Signed and Saved: %s", target) 83 84def main(): 85 """Command Line Invocation Routine""" 86 87 parser = argparse.ArgumentParser(description=""" 88 The data for the interactive plots is stored in the ipython profile. 89 In order to make it accessible when the notebook is published or shared, 90 a github gist of the data is created and the links in the notebook are 91 updated. The library links are also updated to their corresponding publicly 92 accessible URLs. 93 """, 94 prog="publish_interactive_plots.py", formatter_class=RawTextHelpFormatter) 95 96 parser.add_argument( 97 "-p", 98 "--profile", 99 help="ipython profile", 100 default="default", 101 type=str) 102 103 parser.add_argument( 104 "-o", 105 "--outfile", 106 help="name of the output notebook", 107 default="", 108 type=str) 109 110 parser.add_argument("notebook") 111 args = parser.parse_args() 112 113 notebook = args.notebook 114 outfile = args.outfile 115 116 if outfile == "": 117 outfile = "published_" + os.path.basename(notebook) 118 logging.info("Setting outfile as %s", outfile) 119 120 elif not outfile.endswith(".ipynb"): 121 outfile += ".ipynb" 122 123 publish(notebook, outfile) 124 125if __name__ == "__main__": 126 main() 127