Example #1
0
    def __init__(self, name, width, height, interactive, font, logging,  template, host, port, **kwargs):

        # store data
        self.name = '_'.join(name.split())
        d3py_path = os.path.abspath(os.path.dirname(__file__))
        self.filemap = {
            "static/d3.js":{
                "fd":open(d3py_path+"/d3.js","r"), 
                "timestamp":time.time()
            },
        }

        # Networking stuff
        self.host = host
        self.port = port
        self._server_thread = None
        self.httpd = None

        # interactive is True by default as this is designed to be a command line tool
        # we do not want to block interaction after plotting.
        self.interactive = interactive
        self.logging = logging

        # initialise strings
        self.js = JS.JavaScript()
        self.margins = {
            "top": 10, 
            "right": 20, 
            "bottom": 25, 
            "left": 60, 
            "height":height, 
            "width":width
        }
        
        # we use bostock's scheme http://bl.ocks.org/1624660
        self.css = CSS()
        self.html = ""
        self.template = template or "".join(open(d3py_path+'/d3py_template.html').readlines())
        self.js_geoms = JS.JavaScript()
        self.css_geoms = CSS()
        self.geoms = []
        # misc arguments - these go into the css!
        self.font = font
        self.args = {
            "width": width - self.margins["left"] - self.margins["right"],
            "height": height - self.margins["top"] - self.margins["bottom"],
            "font-family": "'%s'; sans-serif"%self.font
        }
        kwargs = dict([(k[0].replace('_','-'), k[1]) for k in kwargs.items()])
        self.args.update(kwargs)
Example #2
0
 def _build_geoms(self):
     '''Build D3py CSS/JS geometries. See /geoms for more details'''
     self.js_geoms = JS.JavaScript()
     self.css_geoms = CSS()
     for geom in self.geoms:
         self.js_geoms.merge(geom._build_js())
         self.css_geoms += geom._build_css()
Example #3
0
    def _save_js(self):
        '''Build file map (dir path and StringIO for output) of data'''
        final_js = JS.JavaScript()
        final_js.merge(self.js)
        final_js.merge(self.js_geoms)

        filename = "%s.js" % self.name
        js = "%s" % final_js
        self.filemap[filename] = {"fd": StringIO(js), "timestamp": time.time()}
Example #4
0
    def _save_js(self):
        # write javascript
        final_js = JS.JavaScript()
        final_js.merge(self.js)
        final_js.merge(self.js_geoms)

        filename = "%s.js"%self.name
        js = "%s"%final_js
        self.filemap[filename] = {"fd":StringIO(js),
                "timestamp":time.time()}
Example #5
0
    def _build_js(self):
        draw = JS.Function("draw", ("data",))
        draw += "var margin = %s;"%json.dumps(self.margins).replace('""','')
        draw += "    width = %s - margin.left - margin.right"%self.margins["width"]
        draw += "    height = %s - margin.top - margin.bottom;"%self.margins["height"]
        # this approach to laying out the graph is from Bostock: http://bl.ocks.org/1624660
        draw += "var g = " + JS.Selection("d3").select("'#chart'") \
            .append("'svg'") \
            .attr("'width'", 'width + margin.left + margin.right + 25') \
            .attr("'height'", 'height + margin.top + margin.bottom + 25') \
            .append("'g'") \
            .attr("'transform'", "'translate(' + margin.left + ',' + margin.top + ')'")

        self.js = JS.JavaScript() + draw + JS.Function("init")
Example #6
0
 def _build_geoms(self):
     self.js_geoms = JS.JavaScript()
     self.css_geoms = CSS()
     for geom in self.geoms:
         self.js_geoms.merge(geom._build_js())
         self.css_geoms += geom._build_css()
Example #7
0
    def __init__(self, name, width, height, interactive, font, logging,
                 template, host, port, **kwargs):
        '''
        Figure is the abstract base class for all figures. Currently 
        subclassed by pandas_figure and networkx_figure. 
        
        Parameters:
        -----------
        name: string
            Name of visualization; will appear in title bar of the webpage, 
            and in the folder where files are stored. 
        width : int 
            Width of the figure in pixels
        height : int 
            Height of the figure in pixels
        interactive : boolean
            Set to false if you are drawing the graph using a script and
            not in the command line. 
        font : string
            Name of the font you'd like to use. See     
            http://www.google.com/webfonts for options
        logging: 
            Logging via the sandard Python loggin library
        template: string
            HTML template for figure. Defaults to /d3py_template (Also, when 
            building your own HTML, please see the default template for 
            correct usage of {{ name }}, {{ host }}, {{ port }}, and 
            {{ font }}
        host: string
            Generally default to 'localhost' for local plotting
        port: int
            Generally defaults to 8000 for local plotting
        
        '''

        # store data
        self.name = '_'.join(name.split())
        d3py_path = os.path.abspath(os.path.dirname(__file__))
        self.filemap = {
            "static/d3.js": {
                "fd": open(d3py_path + "/d3.js", "r"),
                "timestamp": time.time()
            },
        }

        # Networking stuff
        self.host = host
        self.port = port
        self._server_thread = None
        self.httpd = None
        '''Interactive is true by default, as this is designed to be a command
        line tool. We do not want to block interaction after plotting.'''
        self.interactive = interactive
        self.logging = logging

        # initialise strings
        self.js = JS.JavaScript()
        self.margins = {
            "top": 10,
            "right": 20,
            "bottom": 25,
            "left": 60,
            "height": height,
            "width": width
        }

        # we use bostock's scheme http://bl.ocks.org/1624660
        self.css = CSS()
        self.html = ""
        self.template = template or resource_string('d3py',
                                                    'd3py_template.html')
        self.js_geoms = JS.JavaScript()
        self.css_geoms = CSS()
        self.geoms = []
        # misc arguments - these go into the css!
        self.font = font
        self.args = {
            "width": width - self.margins["left"] - self.margins["right"],
            "height": height - self.margins["top"] - self.margins["bottom"],
            "font-family": "'%s'; sans-serif" % self.font
        }

        kwargs = dict([(k[0].replace('_', '-'), k[1]) for k in kwargs.items()])
        self.args.update(kwargs)
Example #8
0
 def setUp(self):
     self.g = javascript.Selection("g").attr("color", "red")
     self.j = javascript.JavaScript() + self.g
     self.f = javascript.Function("test", None, "return 5")