Kapileshwar Singh | 273de83 | 2015-08-20 18:15:29 -0700 | [diff] [blame] | 1 | #!/usr/bin/env python |
Brendan Jackman | e81fdcb | 2017-01-04 17:10:29 +0000 | [diff] [blame] | 2 | # Copyright 2015-2017 ARM Limited |
Kapileshwar Singh | 273de83 | 2015-08-20 18:15:29 -0700 | [diff] [blame] | 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 |
| 19 | The static data is published as an anonymous gist. GitHub does not |
| 20 | allow easy deletions of anonymous gists. |
| 21 | """ |
| 22 | |
Kapileshwar Singh | 273de83 | 2015-08-20 18:15:29 -0700 | [diff] [blame] | 23 | import os |
Kapileshwar Singh | 273de83 | 2015-08-20 18:15:29 -0700 | [diff] [blame] | 24 | import argparse |
Kapileshwar Singh | 273de83 | 2015-08-20 18:15:29 -0700 | [diff] [blame] | 25 | from IPython.nbformat.sign import TrustNotebookApp |
Kapileshwar Singh | 273de83 | 2015-08-20 18:15:29 -0700 | [diff] [blame] | 26 | from argparse import RawTextHelpFormatter |
Kapileshwar Singh | 273de83 | 2015-08-20 18:15:29 -0700 | [diff] [blame] | 27 | |
| 28 | # Logging Configuration |
| 29 | import logging |
| 30 | from trappy.plotter import IPythonConf |
| 31 | |
| 32 | logging.basicConfig(level=logging.INFO) |
| 33 | |
Kapileshwar Singh | 273de83 | 2015-08-20 18:15:29 -0700 | [diff] [blame] | 34 | |
| 35 | def 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"), |
Javi Merino | da88649 | 2016-04-27 14:10:21 +0100 | [diff] [blame] | 43 | IPythonConf.D3_TIP_URL) |
Kapileshwar Singh | 273de83 | 2015-08-20 18:15:29 -0700 | [diff] [blame] | 44 | txt = txt.replace( |
| 45 | IPythonConf.add_web_base("plotter_scripts/EventPlot/d3.v3.min"), |
Javi Merino | da88649 | 2016-04-27 14:10:21 +0100 | [diff] [blame] | 46 | IPythonConf.D3_PLOTTER_URL) |
Kapileshwar Singh | 273de83 | 2015-08-20 18:15:29 -0700 | [diff] [blame] | 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"), |
Javi Merino | da88649 | 2016-04-27 14:10:21 +0100 | [diff] [blame] | 52 | IPythonConf.DYGRAPH_SYNC_URL) |
Kapileshwar Singh | 273de83 | 2015-08-20 18:15:29 -0700 | [diff] [blame] | 53 | txt = txt.replace( |
| 54 | IPythonConf.add_web_base("plotter_scripts/ILinePlot/dygraph-combined"), |
Javi Merino | da88649 | 2016-04-27 14:10:21 +0100 | [diff] [blame] | 55 | IPythonConf.DYGRAPH_COMBINED_URL) |
Kapileshwar Singh | 273de83 | 2015-08-20 18:15:29 -0700 | [diff] [blame] | 56 | txt = txt.replace( |
| 57 | IPythonConf.add_web_base("plotter_scripts/ILinePlot/ILinePlot"), |
| 58 | "https://rawgit.com/sinkap/648927dfd6985d4540a9/raw/69d6f1f9031ae3624c15707315ce04be1a9d1ac3/ILinePlot") |
Kapileshwar Singh | 324d336 | 2015-09-03 12:37:52 +0100 | [diff] [blame] | 59 | txt = txt.replace( |
| 60 | IPythonConf.add_web_base("plotter_scripts/ILinePlot/underscore-min"), |
Javi Merino | da88649 | 2016-04-27 14:10:21 +0100 | [diff] [blame] | 61 | IPythonConf.UNDERSCORE_URL) |
Kapileshwar Singh | 273de83 | 2015-08-20 18:15:29 -0700 | [diff] [blame] | 62 | |
| 63 | logging.info("Updated Library Paths...") |
| 64 | return txt |
| 65 | |
| 66 | |
Javi Merino | fd2e19d | 2016-04-27 14:02:17 +0100 | [diff] [blame] | 67 | def publish(source, target): |
Kapileshwar Singh | 273de83 | 2015-08-20 18:15:29 -0700 | [diff] [blame] | 68 | """Publish the notebook for globally viewable interactive |
| 69 | plots |
| 70 | """ |
| 71 | |
Kapileshwar Singh | 273de83 | 2015-08-20 18:15:29 -0700 | [diff] [blame] | 72 | txt = "" |
| 73 | |
| 74 | with open(source, 'r') as file_fh: |
Javi Merino | fd2e19d | 2016-04-27 14:02:17 +0100 | [diff] [blame] | 75 | txt = change_resource_paths(file_fh.read()) |
Kapileshwar Singh | 273de83 | 2015-08-20 18:15:29 -0700 | [diff] [blame] | 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 | |
Kapileshwar Singh | 273de83 | 2015-08-20 18:15:29 -0700 | [diff] [blame] | 84 | def 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. |
Javi Merino | fd2e19d | 2016-04-27 14:02:17 +0100 | [diff] [blame] | 93 | """, |
Kapileshwar Singh | 273de83 | 2015-08-20 18:15:29 -0700 | [diff] [blame] | 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 | |
Kapileshwar Singh | 273de83 | 2015-08-20 18:15:29 -0700 | [diff] [blame] | 110 | parser.add_argument("notebook") |
| 111 | args = parser.parse_args() |
| 112 | |
Kapileshwar Singh | 273de83 | 2015-08-20 18:15:29 -0700 | [diff] [blame] | 113 | notebook = args.notebook |
| 114 | outfile = args.outfile |
Kapileshwar Singh | 273de83 | 2015-08-20 18:15:29 -0700 | [diff] [blame] | 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 | |
Javi Merino | fd2e19d | 2016-04-27 14:02:17 +0100 | [diff] [blame] | 123 | publish(notebook, outfile) |
Kapileshwar Singh | 273de83 | 2015-08-20 18:15:29 -0700 | [diff] [blame] | 124 | |
| 125 | if __name__ == "__main__": |
| 126 | main() |