Commits

Chris Mutel committed eb87b40

Method explorer. Fixes issue #2

  • Participants
  • Parent commits 7044dcf

Comments (0)

Files changed (3)

File bw2ui/web/app.py

     ms = [{
         "name": " - ".join(key),
         "unit": value["unit"],
-        "num_cfs": value["num_cfs"]
+        "num_cfs": value["num_cfs"],
+        "url": url_for("method_explorer", abbreviation=value['abbreviation'])
     } for key, value in methods.iteritems()]
     ms.sort(key = lambda x: x['name'])
     context = {
 #######################
 
 
-@app.route("/method-json/<name1>/<name2>/<name3>")
-def method_json(name1, name2, name3):
-    name = (name1, name2, name3)
-    print name, name in methods
-    if name not in methods:
+@app.route("/method/<abbreviation>")
+def method_explorer(abbreviation):
+    method = [key for key, value in methods.iteritems()
+        if value['abbreviation'] == abbreviation]
+    if not len(method) == 1:
         abort(404)
-    biosphere = Database("biosphere").load()
-    print "Biosphere loaded", len(biosphere)
-    cfs = [{"n": biosphere[x[0]]["name"],
-        "c": str(biosphere[x[0]]["categories"]),
-        "a": x[1]
-        } for x in Method(name).load().iteritems()]
-    cfs.sort()
-    return json_response(cfs)
-
-
-@app.route("/method/<name1>")
-@app.route("/method/<name1>/<name2>")
-@app.route("/method/<name1>/<name2>/<name3>")
-def method_explorer(name1, name2=None, name3=None):
-    return
+    method = method[0]
+    meta = methods[method]
+    json_data = []
+    for key, value, geo in Method(method).load():
+        flow = Database(key[0]).load()[key]
+        json_data.append({
+            'name': flow['name'],
+            'unit': flow.get('unit', ''),
+            'categories': ",".join(flow.get('categories', [])),
+            'cf': value['amount'] if isinstance(value, dict) else value,
+            'location': geo,
+            'url': url_for('json_editor', database=key[0], code=key[1])
+        })
+    json_data.sort(key=lambda x: x['name'])
+    return render_template(
+        "method.html",
+        name=method,
+        unit=meta['unit'],
+        description=meta['description'],
+        data=JsonWrapper.dumps(json_data)
+    )
 
 
 # use werkzeug.utils.secure_filename to check uploaded file names

File bw2ui/web/templates/jsoneditor.html

             font: 10.5pt arial;
             color: #4d4d4d;
             line-height: 150%;
-            width: 500px;
+            margin: 0;
         }
 
         code {
             width: 100%;
             height: 800px;
         }
+
+        .container {
+            width: 600px;
+            margin: 50px auto 0 auto;
+        }
     </style>
 </head>
 <body>
-
-<p>
-    Switch editor mode using the mode box.
-    Note that the mode can be changed programmatically as well using the method
-    <code>editor.setMode(mode)</code>, try it in the console of your browser.
-</p>
-
-<div id="jsoneditor"></div>
-
+<div class="container">
+    <div id="jsoneditor"></div>
+</div>
 <script type="text/javascript" >
     var container = document.getElementById('jsoneditor');
 

File bw2ui/web/templates/method.html

+{% extends "base.html" %}
+
+{% block body %}
+<h1>Method{% for obj in name %}: {{ obj }}{% endfor %}</h1>
+<h2>Unit: {{ unit }}</h2>
+<h2>Description:</h2>
+<p>{{ description }}</p>
+
+<div id="bgtable"></div>
+
+<script type="text/javascript">
+var columns = [{
+    name: "name",
+    label: "Name",
+    cell: "string",
+    editable: false
+}, {
+    name: "unit",
+    label: "Unit",
+    cell: "string",
+    editable: false
+}, {
+    name: "categories",
+    label: "Categories",
+    cell: "string",
+    editable: false
+}, {
+    name: "location",
+    label: "Location",
+    cell: "string",
+    editable: false
+}, {
+    name: "cf",
+    label: "CF",
+    cell: "number",
+    editable: false
+}];
+
+var data = {{ data|safe }};
+
+var callback = function (model) {
+    window.location = model.attributes.url;
+};
+
+BackgridTable(data, columns, "#bgtable", ['name'], "Filter by name", 50, callback);
+</script>
+{% endblock %}