Skip to content

Commit 7e3950f

Browse files
authored
Merge pull request #373 from informatics-lab/forestjs
ForestJS
2 parents a09444c + 15be2d6 commit 7e3950f

File tree

10 files changed

+114
-18
lines changed

10 files changed

+114
-18
lines changed

forest/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
.. automodule:: forest.presets
2424
2525
"""
26-
__version__ = '0.16.1'
26+
__version__ = '0.16.2'
2727

2828
from .config import *
2929
from . import (

forest/js/forest.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
const helloWorld = () => "Hello, World!"
2+
3+
4+
// Populate variable options from dataset value
5+
const link_selects = function(dataset_select, variable_select, source) {
6+
let label = dataset_select.value;
7+
if (label !== "") {
8+
let index = source.data['datasets'].indexOf(label)
9+
let defaults = ["Please specify"];
10+
variable_select.options = defaults.concat(
11+
source.data['variables'][index]);
12+
}
13+
}
14+
15+
16+
module.exports = {
17+
helloWorld,
18+
link_selects
19+
}

forest/js/forest.test.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
const forest = require("./forest")
2+
3+
4+
describe("helloWorld", () => {
5+
it("should return message", () => {
6+
expect(forest.helloWorld()).toEqual("Hello, World!")
7+
})
8+
})

forest/js/main.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
window.forest = require('./forest')
2+

forest/js/package.json

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"name": "forestjs",
3+
"version": "0.0.1",
4+
"description": "Helper JS for FOREST Python library",
5+
"main": "index.js",
6+
"dependencies": {
7+
"redux": "^4.0.5"
8+
},
9+
"devDependencies": {
10+
"browserify": "^16.5.1",
11+
"jest": "^26.0.1"
12+
},
13+
"scripts": {
14+
"test": "jest",
15+
"watch": "jest --watch ./*.test.js",
16+
"build": "browserify main.js -o ../static/forest-min.js"
17+
},
18+
"author": "andrewgryan",
19+
"license": "BSD-3-Clause"
20+
}

forest/static/script.js

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,3 @@ let openId = function(id) {
44
let closeId = function(id) {
55
document.getElementById(id).style.width = "0";
66
}
7-
8-
let forest = (function() {
9-
let ns = {};
10-
11-
// Populate variable options from dataset value
12-
ns.link_selects = function(dataset_select, variable_select, source) {
13-
let label = dataset_select.value;
14-
if (label !== "") {
15-
let index = source.data['datasets'].indexOf(label)
16-
let defaults = ["Please specify"];
17-
variable_select.options = defaults.concat(
18-
source.data['variables'][index]);
19-
}
20-
}
21-
22-
return ns;
23-
})();

forest/templates/index.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
{% block postamble %}
1515
<link rel="stylesheet" href="forest/static/style.css" type="text/css" media="all">
1616
<script src="forest/static/script.js" charset="utf-8"></script>
17+
<script src="forest/static/forest-min.js" charset="utf-8"></script>
1718
{% endblock %}
1819

1920
{% block contents %}

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
bokeh>=2.0.1 # Port to 2.0.0 in future
2+
nodejs>=10.13
23
datashader
34
h5netcdf
45
iris

setup.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,11 @@
33
"""
44
import os
55
import re
6+
import subprocess
67
import setuptools
8+
import setuptools.command.build_py
9+
import setuptools.command.develop
10+
import setuptools.command.install
711

812

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

2933

34+
def build_js(command_subclass):
35+
"""Decorator to call npm install and npm run build"""
36+
subclass_run = command_subclass.run
37+
def run(self):
38+
self.run_command("build_js")
39+
subclass_run(self)
40+
command_subclass.run = run
41+
return command_subclass
42+
43+
44+
@build_js
45+
class InstallCommand(setuptools.command.install.install):
46+
"""Python and JS code"""
47+
48+
49+
@build_js
50+
class DevelopCommand(setuptools.command.develop.develop):
51+
"""Python and JS code"""
52+
53+
54+
@build_js
55+
class BuildPyCommand(setuptools.command.build_py.build_py):
56+
"""Python and JS code"""
57+
58+
59+
class BuildJSCommand(setuptools.command.build_py.build_py):
60+
"""Use nodejs and npm commands to browserify forest.js
61+
62+
.. note:: Assume current working directory is package ROOT
63+
"""
64+
def run(self):
65+
cwd = os.getcwd()
66+
os.chdir("forest/js")
67+
if not os.path.exists("node_modules"):
68+
subprocess.check_call(["npm", "install"])
69+
subprocess.check_call(["npm", "run", "build"])
70+
os.chdir(cwd)
71+
super().run()
72+
73+
3074
setuptools.setup(
3175
name=NAME,
3276
version=find_version(),
3377
author="Andrew Ryan",
3478
author_email="[email protected]",
79+
cmdclass={
80+
"install": InstallCommand,
81+
"develop": DevelopCommand,
82+
"build_py": BuildPyCommand,
83+
"build_js": BuildJSCommand,
84+
},
3585
description="Forecast visualisation and survey tool",
3686
packages=setuptools.find_packages(),
3787
package_data={

test/test_js.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import os
2+
import subprocess
3+
4+
5+
JS_DIR = os.path.join(os.path.dirname(__file__), "../forest/js")
6+
7+
8+
def test_forestjs():
9+
cwd = os.getcwd()
10+
os.chdir(JS_DIR)
11+
subprocess.check_call(["npm", "test"])
12+
os.chdir(cwd)

0 commit comments

Comments
 (0)