From f122e5240db2fc1f2efe444a36be03caba6877af Mon Sep 17 00:00:00 2001 From: Lukas Vacek Date: Tue, 1 Aug 2017 22:36:32 +0200 Subject: [PATCH] offline mode --- highcharts/highcharts/highcharts.py | 34 ++++++++++++++++++++++++----- 1 file changed, 28 insertions(+), 6 deletions(-) diff --git a/highcharts/highcharts/highcharts.py b/highcharts/highcharts/highcharts.py index 0b7a5a3..e7c1ef6 100644 --- a/highcharts/highcharts/highcharts.py +++ b/highcharts/highcharts/highcharts.py @@ -10,6 +10,7 @@ import json, uuid import re import datetime +import urllib2 import html from collections import Iterable from .options import BaseOptions, ChartOptions, ColorAxisOptions, \ @@ -51,6 +52,9 @@ def __init__(self, **kwargs): This is the base class for all the charts. The following keywords are accepted: :keyword: **display_container** - default: ``True`` + **offline - default: ``False`` + If True, download all .js and .css file and put them + into the generated .html so it can be viewed offline. """ # set the model self.model = self.__class__.__name__ #: The chart model, @@ -59,6 +63,7 @@ def __init__(self, **kwargs): # an Instance of Jinja2 template self.template_page_highcharts = template_page self.template_content_highcharts = template_content + # set Javascript src, Highcharts lib needs to make sure it's up to date self.JSsource = [ @@ -74,6 +79,9 @@ def __init__(self, **kwargs): 'https://www.highcharts.com/highslide/highslide.css', ] + + self.offline = kwargs.get("offline", False) + # set data self.data = [] self.data_temp = [] @@ -323,13 +331,27 @@ def buildhtmlheader(self): if self.drilldown_flag: self.add_JSsource('http://code.highcharts.com/modules/drilldown.js') - self.header_css = [ - '' % h for h in self.CSSsource - ] - self.header_js = [ - '' % h for h in self.JSsource - ] + + if self.offline: + opener = urllib2.build_opener() + opener.addheaders = [('User-Agent', 'Mozilla/5.0')] + + self.header_css = [ + '' % opener.open(h).read() for h in self.CSSsource + ] + + self.header_js = [ + '' % opener.open(h).read() for h in self.JSsource + ] + else: + self.header_css = [ + '' % h for h in self.CSSsource + ] + + self.header_js = [ + '' % h for h in self.JSsource + ] self.htmlheader = '' for css in self.header_css: