diff --git a/datascience/__init__.py b/datascience/__init__.py new file mode 100644 index 000000000..50e2085e4 --- /dev/null +++ b/datascience/__init__.py @@ -0,0 +1,2 @@ +from .table import * +from .maps import * diff --git a/datascience/maps/__init__.py b/datascience/maps/__init__.py new file mode 100644 index 000000000..94f8cafc8 --- /dev/null +++ b/datascience/maps/__init__.py @@ -0,0 +1,4 @@ +from .loader import * +from .maps import * + +init_js() diff --git a/datascience/maps/js/maps.js b/datascience/maps/js/maps.js new file mode 100644 index 000000000..66fa90c69 --- /dev/null +++ b/datascience/maps/js/maps.js @@ -0,0 +1,13 @@ +// Registers the Backbone view for the Polymap widget, although for now this is +// a dummy view until we can get everything hooked up properly. +// TODO(sam): Make this actually do things. + +require(["widgets/js/widget", "widgets/js/manager"], function(widget, manager) { + var TestWidgetView = widget.DOMWidgetView.extend({ + render: function() { + this.$el.text("Hello world!"); + } + }); + + manager.WidgetManager.register_widget_view('TestWidgetView', TestWidgetView); +}); diff --git a/datascience/maps/loader.py b/datascience/maps/loader.py new file mode 100644 index 000000000..44e561370 --- /dev/null +++ b/datascience/maps/loader.py @@ -0,0 +1,13 @@ +from IPython.display import display_javascript + +__maps_js_has_initialized__ = False + +# Uses a hack to load the Javascript needed to render the map. Becaose of this, +# calls to draw_map cannot be in the same cell as the import statement. +# +# Maybe it's a good idea to put this login in a line magic? +def init_js(): + global __maps_js_has_initialized__ + if not __maps_js_has_initialized__: + # Keep in sync with the the data_files variable in setup.py + display_javascript("IPython.load_extensions('datascience_js/maps')") diff --git a/datascience/maps/maps.py b/datascience/maps/maps.py new file mode 100644 index 000000000..4d1dea5cb --- /dev/null +++ b/datascience/maps/maps.py @@ -0,0 +1,34 @@ +from IPython.html import widgets # Widget definitions +from IPython.utils.traitlets import Unicode + +""" +This file contains the Python backend to draw maps with overlaid polygons. +TODO(sam): Actually make this draw maps +""" + +class TestWidget(widgets.DOMWidget): + _view_name = Unicode('TestWidgetView', sync=True) + _view_module = Unicode('maps', sync=True) + + +def draw_map(center, zoom, points=[], regions=[]): + """Draw a map with center & zoom containing all points and + regions that displays points as circles and regions as polygons. + + center -- lat-long pair at the center of the map + zoom -- zoom level + points -- a sequence of MapPoints + regions -- a sequence of MapRegions + """ + # Some magic to get javascript to display the map + +class MapPoint: + """A circle https://developers.google.com/maps/documentation/javascript/shapes#circles""" + def __init__(self, center, radius, strokeColor, strokeOpacity, strokeWeight, fillColor, fillOpacity): + pass + +class MapRegion: + """A polygon https://developers.google.com/maps/documentation/javascript/shapes#polygons""" + def __init__(self, paths, strokeColor, strokeOpacity, strokeWeight, fillColor, fillOpacity): + """paths -- a list of lat-long pairs or a list of list of lat-long pairs""" + pass diff --git a/datascience.py b/datascience/table.py similarity index 99% rename from datascience.py rename to datascience/table.py index 85750a5f7..ea44a997c 100644 --- a/datascience.py +++ b/datascience/table.py @@ -667,7 +667,6 @@ def __len__(self): def __repr__(self): return '{0}({1})'.format(type(self).__name__, repr(self._table)) - def _zero_on_type_error(column_fn): """Wrap a function on an np.ndarray to return 0 on a type error.""" @functools.wraps(column_fn) diff --git a/setup.py b/setup.py index 162ca2088..14de7ebd2 100644 --- a/setup.py +++ b/setup.py @@ -1,5 +1,7 @@ import sys -from distutils.core import setup +import os +import IPython +from setuptools import setup from setuptools.command.test import test as TestCommand install_requires = [ @@ -31,15 +33,23 @@ def run_tests(self): sys.exit(errno) +# Installs GMaps Javascript in the nbextensions folder for loading. +# We load this file using IPython.load_extensions('datascience_js/maps') in +# Javascript. Keep in sync with the path in maps/leader.py +ipython_dir = IPython.utils.path.get_ipython_dir() +data_files = [(os.path.join(ipython_dir, "nbextensions/datascience_js"), + ["datascience/maps/js/maps.js"] )] + setup( name = 'datascience', - py_modules = ['datascience'], - version = '0.2.1', + packages = ['datascience'], + version = '0.2.2', install_requires = install_requires, tests_require = test_requires, + data_files = data_files, cmdclass = {'test': PyTest}, - description = 'A Python library for introductory data science', - author = 'John DeNero, David Culler, Alvin Wan', + description = 'A Jupyter notebook Python library for introductory data science', + author = 'John DeNero, David Culler, Alvin Wan, Sam Lau', author_email = 'ds-instr@berkeley.edu', url = 'https://github.com/dsten/datascience', download_url = 'https://github.com/dsten/datascience/archive/0.2.0.zip',