blob: 7711556a63282bf4cebd93cfc31a6189a3e28d78 [file] [log] [blame]
Kapileshwar Singh273de832015-08-20 18:15:29 -07001#!/usr/bin/env python
Brendan Jackmane81fdcb2017-01-04 17:10:29 +00002# Copyright 2015-2017 ARM Limited
Kapileshwar Singh273de832015-08-20 18:15:29 -07003#
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
Kapileshwar Singh273de832015-08-20 18:15:29 -070023import os
Kapileshwar Singh273de832015-08-20 18:15:29 -070024import argparse
Kapileshwar Singh273de832015-08-20 18:15:29 -070025from IPython.nbformat.sign import TrustNotebookApp
Kapileshwar Singh273de832015-08-20 18:15:29 -070026from argparse import RawTextHelpFormatter
Kapileshwar Singh273de832015-08-20 18:15:29 -070027
28# Logging Configuration
29import logging
30from trappy.plotter import IPythonConf
31
32logging.basicConfig(level=logging.INFO)
33
Kapileshwar Singh273de832015-08-20 18:15:29 -070034
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"),
Javi Merinoda886492016-04-27 14:10:21 +010043 IPythonConf.D3_TIP_URL)
Kapileshwar Singh273de832015-08-20 18:15:29 -070044 txt = txt.replace(
45 IPythonConf.add_web_base("plotter_scripts/EventPlot/d3.v3.min"),
Javi Merinoda886492016-04-27 14:10:21 +010046 IPythonConf.D3_PLOTTER_URL)
Kapileshwar Singh273de832015-08-20 18:15:29 -070047 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 Merinoda886492016-04-27 14:10:21 +010052 IPythonConf.DYGRAPH_SYNC_URL)
Kapileshwar Singh273de832015-08-20 18:15:29 -070053 txt = txt.replace(
54 IPythonConf.add_web_base("plotter_scripts/ILinePlot/dygraph-combined"),
Javi Merinoda886492016-04-27 14:10:21 +010055 IPythonConf.DYGRAPH_COMBINED_URL)
Kapileshwar Singh273de832015-08-20 18:15:29 -070056 txt = txt.replace(
57 IPythonConf.add_web_base("plotter_scripts/ILinePlot/ILinePlot"),
58 "https://rawgit.com/sinkap/648927dfd6985d4540a9/raw/69d6f1f9031ae3624c15707315ce04be1a9d1ac3/ILinePlot")
Kapileshwar Singh324d3362015-09-03 12:37:52 +010059 txt = txt.replace(
60 IPythonConf.add_web_base("plotter_scripts/ILinePlot/underscore-min"),
Javi Merinoda886492016-04-27 14:10:21 +010061 IPythonConf.UNDERSCORE_URL)
Kapileshwar Singh273de832015-08-20 18:15:29 -070062
63 logging.info("Updated Library Paths...")
64 return txt
65
66
Javi Merinofd2e19d2016-04-27 14:02:17 +010067def publish(source, target):
Kapileshwar Singh273de832015-08-20 18:15:29 -070068 """Publish the notebook for globally viewable interactive
69 plots
70 """
71
Kapileshwar Singh273de832015-08-20 18:15:29 -070072 txt = ""
73
74 with open(source, 'r') as file_fh:
Javi Merinofd2e19d2016-04-27 14:02:17 +010075 txt = change_resource_paths(file_fh.read())
Kapileshwar Singh273de832015-08-20 18:15:29 -070076
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 Singh273de832015-08-20 18:15:29 -070084def 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 Merinofd2e19d2016-04-27 14:02:17 +010093 """,
Kapileshwar Singh273de832015-08-20 18:15:29 -070094 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 Singh273de832015-08-20 18:15:29 -0700110 parser.add_argument("notebook")
111 args = parser.parse_args()
112
Kapileshwar Singh273de832015-08-20 18:15:29 -0700113 notebook = args.notebook
114 outfile = args.outfile
Kapileshwar Singh273de832015-08-20 18:15:29 -0700115
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 Merinofd2e19d2016-04-27 14:02:17 +0100123 publish(notebook, outfile)
Kapileshwar Singh273de832015-08-20 18:15:29 -0700124
125if __name__ == "__main__":
126 main()