brightway2-ui / bw2ui / web /

# -*- coding: utf-8 -*-
from brightway2 import config, databases, methods, Database, Method, \
from bw2analyzer import ContributionAnalysis
from bw2calc import LCA
from flask import Flask, url_for, render_template, request, redirect, abort, \
from jobs import JobDispatch, InvalidJob
from utils import get_job_id, get_job, set_job_status
import base64

app = Flask(__name__)

def page_not_found(e):
    # Log error?
    return render_template('404.html'), 404

def internal_error(e):
    # Log error?
    return render_template('500.html'), 500

def job_status(job):
        return Response(JsonWrapper.dumps(get_job(job)),

def job_dispatch(job):
        job_data = get_job(job)
        return JobDispatch()(job, **job_data)
    except InvalidJob:

def index():
    context = {
        'databases': databases,
        'methods': methods,
        'config': config
    if config.is_temp_dir:
        context["redirect"] = url_for(start_bw)
    return render_template("index.html", **context)

def lca(process=None, method=None):
    context = {}
    if process:
        method = eval(base64.urlsafe_b64decode(str(method)), None, None)
        process = eval(base64.urlsafe_b64decode(str(process)), None, None)
        lca = LCA(process, method)
        rt, rb = lca.reverse_dict()
        ca = ContributionAnalysis()
        context["treemap_data"] = JsonWrapper.dumps(ca.d3_treemap(
  , rb, rt))
        context["ia_score"] = "%.2g" % lca.score
        context["ia_unit"] = methods[method]["unit"]
        context["ia_method"] = ": ".join(method)
        context["fu"] = [(ca.get_name(k), "%.2g" % v, ca.db_names[k[0]][k][
            "unit"]) for k, v in process.iteritems()]
        return render_template("lca.html", **context)
        return "No parameters"

def progress_test():
    job_id = get_job_id()
    status_id = get_job_id()
    set_job_status(job_id, {"name": "progress-test", "status": status_id})
    set_job_status(status_id, {"status": "Starting..."})
    return render_template("progress.html", **{"job": job_id, 'status': status_id})

@app.route('/start', methods=["GET", "POST"])
def start_bw():
    """Start Brightway"""
    job_id = get_job_id()
    if request.method == "GET":
    elif not request.form["confirm"] == "false":
        return redirect(url_for(index) + "?temp_dir_ok=True")
    # Starting Brightway
    # If POST[action] = "get_biosphere":
    set_job_status(job_id, {"action": "biosphere-import", "finished": False})
    # Download file to disk
    set_job_status(job_id, {"action": "biosphere-import", "finished": True})

    # Step two
    # Import IA methods
    # Can also be from dropbox

    # Step three
    # Import Ecoinvent or other LCI database

    # Step four
    # There is no step four!
    # Actually, there is: go to the normal homepage

# Normal homepage:
# Think of what should be here
# At a minimum, a table of databases
# and IA methods
# A quick entry to LCA (define function units)

# use werkzeug.utils.secure_filename to check uploaded file names

# to send static files not from 'static': send_from_directory
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.