Skip to content

Commit

Permalink
Merge pull request mikedewar#56 from andrewcstewart/master
Browse files Browse the repository at this point in the history
Parameterized hostname
  • Loading branch information
mikedewar committed Mar 17, 2013
2 parents a63ef75 + 0f21a74 commit 1593190
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 15 deletions.
8 changes: 4 additions & 4 deletions d3py/d3py_template.html
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<head>
<meta http-equiv="content-type" content="text/html;charset=utf-8;no-cache">
<script type="text/javascript" charset="utf-8" src="http://localhost:{{ port }}/static/d3.js"></script>
<script type="text/javascript" charset="utf-8" src="http://localhost:{{ port }}/{{ name }}.js"></script>
<link type="text/css" rel="stylesheet" href="http://localhost:{{ port }}/{{ name }}.css">
<script type="text/javascript" charset="utf-8" src="http://{{ host }}:{{ port }}/static/d3.js"></script>
<script type="text/javascript" charset="utf-8" src="http://{{ host }}:{{ port }}/{{ name }}.js"></script>
<link type="text/css" rel="stylesheet" href="http://{{ host }}:{{ port }}/{{ name }}.css">
<link href='http://fonts.googleapis.com/css?family={{ font }}' rel='stylesheet' type='text/css'>

<title>d3py: {{ name }}</title>
Expand All @@ -11,7 +11,7 @@
<body>
<div id="chart"></div>
<script>
d3.json("http://localhost:{{ port }}/{{ name }}.json", draw);
d3.json("http://{{ host }}:{{ port }}/{{ name }}.json", draw);
</script>
</body>

Expand Down
19 changes: 13 additions & 6 deletions d3py/figure.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import logging
import webbrowser
from HTTPHandler import CustomHTTPRequestHandler, ThreadedHTTPServer
import IPython.core.display
import threading
from cStringIO import StringIO
import time
Expand All @@ -11,7 +12,7 @@
import javascript as JS

class Figure(object):
def __init__(self, name, width, height, interactive, font, logging, template, port, **kwargs):
def __init__(self, name, width, height, interactive, font, logging, template, host, port, **kwargs):

# store data
self.name = '_'.join(name.split())
Expand All @@ -24,6 +25,7 @@ def __init__(self, name, width, height, interactive, font, logging, template, p
}

# Networking stuff
self.host = host
self.port = port
self._server_thread = None
self.httpd = None
Expand Down Expand Up @@ -195,6 +197,7 @@ def _save_js(self):
def _save_html(self):
# update the html with the correct port number
self.html = self.html.replace("{{ port }}", str(self.port))
self.html = self.html.replace("{{ host }}", str(self.host))
# write html
filename = "%s.html"%self.name
self.filemap[filename] = {"fd":StringIO(self.html),
Expand All @@ -214,14 +217,18 @@ def show(self, interactive=None):
# if not blocking, we serve the
self._serve(blocking=False)
# fire up a browser
webbrowser.open_new_tab("http://localhost:%s/%s.html"%(self.port, self.name))
webbrowser.open_new_tab("http://%s:%s/%s.html"%(self.host,self.port, self.name))

def display(self, width=700, height=400):
html = "<iframe src=http://%s:%s/%s.html width=%s height=%s>" %(self.host, self.port, self.name, width, height)
IPython.core.display.HTML(html)

def _serve(self, blocking=True):
"""
start up a server to serve the files for this vis.
"""
msgparams = (self.port, self.name)
url = "http://localhost:%s/%s.html"%msgparams
msgparams = (self.host, self.port, self.name)
url = "http://%s:%s/%s.html"%msgparams
if self._server_thread is None or self._server_thread.active_count() == 0:
Handler = CustomHTTPRequestHandler
Handler.filemap = self.filemap
Expand All @@ -232,14 +239,14 @@ def _serve(self, blocking=True):
print "Exception %s"%e
return False
if blocking:
logging.info('serving forever on port: %s'%msgparams[0])
logging.info('serving forever on port: %s'%msgparams[1])
msg = "You can find your chart at " + url
print msg
print "Ctrl-C to stop serving the chart and quit!"
self._server_thread = None
self.httpd.serve_forever()
else:
logging.info('serving asynchronously on port %s'%msgparams[0])
logging.info('serving asynchronously on port %s'%msgparams[1])
self._server_thread = threading.Thread(
target=self.httpd.serve_forever
)
Expand Down
4 changes: 2 additions & 2 deletions d3py/pandas_figure.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
class PandasFigure(Figure):
def __init__(self, data, name="figure", width=800, height=400,
interactive=True, font="Asap", logging=False, template=None,
port=8000, **kwargs):
host="localhost", port=8000, **kwargs):
"""
data : dataFrame
pandas dataFrame used for the plot. This dataFrame is column centric
Expand All @@ -34,7 +34,7 @@ def __init__(self, data, name="figure", width=800, height=400,
super(PandasFigure, self).__init__(
name=name, width=width, height=height,
interactive=interactive, font=font, logging=logging, template=template,
port=port, **kwargs
host=host, port=port, **kwargs
)
# store data
self.data = data
Expand Down
6 changes: 3 additions & 3 deletions d3py/templates.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
d3py_template = '''<html>
<head>
<script type="text/javascript" src="http://mbostock.github.com/d3/d3.js"></script>
<script type="text/javascript" src="http://localhost:{{ port }}/{{ name }}.js"></script>
<link type="text/css" rel="stylesheet" href="http://localhost:{{ port }}/{{ name }}.css">
<script type="text/javascript" src="http://{{ host }}:{{ port }}/{{ name }}.js"></script>
<link type="text/css" rel="stylesheet" href="http://{{ host }}:{{ port }}/{{ name }}.css">
<link href='http://fonts.googleapis.com/css?family={{ font }}' rel='stylesheet' type='text/css'>
<title>d3py: {{ name }}</title>
Expand All @@ -11,7 +11,7 @@
<body>
<div id="chart"></div>
<script>
d3.json("http://localhost:{{ port }}/{{ name }}.json", draw);
d3.json("http://{{ host }}:{{ port }}/{{ name }}.json", draw);
</script>
</body>
Expand Down

0 comments on commit 1593190

Please sign in to comment.