Skip to content

Commit

Permalink
Merge pull request #373 from informatics-lab/forestjs
Browse files Browse the repository at this point in the history
ForestJS
  • Loading branch information
andrewgryan authored May 14, 2020
2 parents a09444c + 15be2d6 commit 7e3950f
Show file tree
Hide file tree
Showing 10 changed files with 114 additions and 18 deletions.
2 changes: 1 addition & 1 deletion forest/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
.. automodule:: forest.presets
"""
__version__ = '0.16.1'
__version__ = '0.16.2'

from .config import *
from . import (
Expand Down
19 changes: 19 additions & 0 deletions forest/js/forest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
const helloWorld = () => "Hello, World!"


// Populate variable options from dataset value
const link_selects = function(dataset_select, variable_select, source) {
let label = dataset_select.value;
if (label !== "") {
let index = source.data['datasets'].indexOf(label)
let defaults = ["Please specify"];
variable_select.options = defaults.concat(
source.data['variables'][index]);
}
}


module.exports = {
helloWorld,
link_selects
}
8 changes: 8 additions & 0 deletions forest/js/forest.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
const forest = require("./forest")


describe("helloWorld", () => {
it("should return message", () => {
expect(forest.helloWorld()).toEqual("Hello, World!")
})
})
2 changes: 2 additions & 0 deletions forest/js/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
window.forest = require('./forest')

20 changes: 20 additions & 0 deletions forest/js/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"name": "forestjs",
"version": "0.0.1",
"description": "Helper JS for FOREST Python library",
"main": "index.js",
"dependencies": {
"redux": "^4.0.5"
},
"devDependencies": {
"browserify": "^16.5.1",
"jest": "^26.0.1"
},
"scripts": {
"test": "jest",
"watch": "jest --watch ./*.test.js",
"build": "browserify main.js -o ../static/forest-min.js"
},
"author": "andrewgryan",
"license": "BSD-3-Clause"
}
17 changes: 0 additions & 17 deletions forest/static/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,3 @@ let openId = function(id) {
let closeId = function(id) {
document.getElementById(id).style.width = "0";
}

let forest = (function() {
let ns = {};

// Populate variable options from dataset value
ns.link_selects = function(dataset_select, variable_select, source) {
let label = dataset_select.value;
if (label !== "") {
let index = source.data['datasets'].indexOf(label)
let defaults = ["Please specify"];
variable_select.options = defaults.concat(
source.data['variables'][index]);
}
}

return ns;
})();
1 change: 1 addition & 0 deletions forest/templates/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
{% block postamble %}
<link rel="stylesheet" href="forest/static/style.css" type="text/css" media="all">
<script src="forest/static/script.js" charset="utf-8"></script>
<script src="forest/static/forest-min.js" charset="utf-8"></script>
{% endblock %}

{% block contents %}
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
bokeh>=2.0.1 # Port to 2.0.0 in future
nodejs>=10.13
datashader
h5netcdf
iris
Expand Down
50 changes: 50 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@
"""
import os
import re
import subprocess
import setuptools
import setuptools.command.build_py
import setuptools.command.develop
import setuptools.command.install


NAME = "forest"
Expand All @@ -27,11 +31,57 @@ def load(fname):
return result


def build_js(command_subclass):
"""Decorator to call npm install and npm run build"""
subclass_run = command_subclass.run
def run(self):
self.run_command("build_js")
subclass_run(self)
command_subclass.run = run
return command_subclass


@build_js
class InstallCommand(setuptools.command.install.install):
"""Python and JS code"""


@build_js
class DevelopCommand(setuptools.command.develop.develop):
"""Python and JS code"""


@build_js
class BuildPyCommand(setuptools.command.build_py.build_py):
"""Python and JS code"""


class BuildJSCommand(setuptools.command.build_py.build_py):
"""Use nodejs and npm commands to browserify forest.js
.. note:: Assume current working directory is package ROOT
"""
def run(self):
cwd = os.getcwd()
os.chdir("forest/js")
if not os.path.exists("node_modules"):
subprocess.check_call(["npm", "install"])
subprocess.check_call(["npm", "run", "build"])
os.chdir(cwd)
super().run()


setuptools.setup(
name=NAME,
version=find_version(),
author="Andrew Ryan",
author_email="[email protected]",
cmdclass={
"install": InstallCommand,
"develop": DevelopCommand,
"build_py": BuildPyCommand,
"build_js": BuildJSCommand,
},
description="Forecast visualisation and survey tool",
packages=setuptools.find_packages(),
package_data={
Expand Down
12 changes: 12 additions & 0 deletions test/test_js.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import os
import subprocess


JS_DIR = os.path.join(os.path.dirname(__file__), "../forest/js")


def test_forestjs():
cwd = os.getcwd()
os.chdir(JS_DIR)
subprocess.check_call(["npm", "test"])
os.chdir(cwd)

0 comments on commit 7e3950f

Please sign in to comment.