def builds(): focus = request.args.get('focus') build_args = {} build_args["classification"] = request.args.get('classification', 'hs92') build_args["origin_id"] = request.args.get('origin_id') or request.args.get('dest_id') build_args["dest_id"] = request.args.get('dest_id') or None build_args["prod_id"] = request.args.get('prod_id') or None build_args["year"] = request.args.get('year', available_years[build_args["classification"]][-1]) build_args["defaults"] = {"origin":"nausa", "dest":"aschn", "prod":"010101"} build_args["viz"] = ["tree_map", "rings"] if build_args["origin_id"] == build_args["dest_id"]: origin_dest_prod = get_origin_dest_prod(None, build_args["dest_id"], build_args["prod_id"], build_args["classification"], [build_args["year"]], "export") build_args["dest_id"] = origin_dest_prod[0].id_3char all_builds = get_all_builds(**build_args) if build_args["origin_id"] and build_args["dest_id"]: build_args["origin_id"], build_args["dest_id"] = build_args["dest_id"], build_args["origin_id"] all_builds = get_all_builds(**build_args) + all_builds ''' Need some way of ranking these build... ''' for build in all_builds: build.relevance = 0 for var in ["trade_flow", "origin_id", "dest_id", "prod_id"]: build_arg_var = build_args.get(var) or request.args.get(var) build_var = getattr(build, var.replace("_id", "")) if hasattr(build_var, "get_display_id"): if build_var.get_display_id() == build_arg_var: build.relevance += 2 else: if build_arg_var == build_var: build.relevance += 1 all_builds.sort(key=lambda x: x.relevance, reverse=True) # raise Exception(all_builds[0]) if focus == "origin_id": attr = Country.query.filter_by(id_3char=build_args["origin_id"]).first() or Country.query.filter(Country.id.endswith(build_args["origin_id"])).first() elif focus == "dest_id": attr = Country.query.filter_by(id_3char=build_args["dest_id"]).first() or Country.query.filter(Country.id.endswith(build_args["dest_id"])).first() elif focus == "prod_id": tbl = globals()[build_args["classification"].title()] c = build_args["classification"] attr = tbl.query.filter(getattr(tbl,c)==build_args["prod_id"]).first() attr_name = attr.get_name() profile = { "title":gettext("Profile for %(attr)s", attr=attr_name), "url":attr.get_profile_url(), "icon":attr.get_icon(), "color":attr.color, "name":attr_name } return jsonify(profile=profile, builds=[{"title": b.question(), "url": b.url(), "viz": b.viz["slug"]} for b in all_builds])
def get_builds(self): origins, dests, products = [[None]] * 3 if len(self.countries): origins = self.countries[0] if len(self.countries) > 1: dests = self.countries[1] if len(self.hs) or len(self.sitc): products = sum([self.hs, self.sitc], []) combos = itertools.product([self.trade_flow], origins, dests, products) combos = [c for c in combos if len(set(filter(None, c))) == len(filter(None, c))] def get_default(looking_for, have, trade_flow, classification, year): if looking_for == "dest": entity = getattr(db_data, "{}_models".format(classification)).Yod.query \ .filter_by(origin=have) \ .filter_by(year=available_years[classification][-1]) \ .order_by(desc(trade_flow+"_val")).limit(1).first() elif looking_for == "origin": entity = getattr(db_data, "{}_models".format(classification)).Yop.query \ .filter_by(product=have) \ .filter_by(year=available_years[classification][-1]) \ .order_by(desc(trade_flow+"_val")).limit(1).first() elif looking_for == "product": entity = getattr(db_data, "{}_models".format(classification)).Yop.query \ .filter_by(origin=have) \ .filter_by(year=available_years[classification][-1]) \ .order_by(desc(trade_flow+"_val")).limit(1).first() if entity: return getattr(entity, looking_for) exact = [] for combo in combos[:4]: trade_flow, origin, dest, product = combo origin = origin or get_default("origin", dest, "export", "hs92", self.year) dest = dest or get_default("dest", origin, "export", "hs92", self.year) product = product or get_default("product", origin, "export", "hs92", self.year) # raise Exception([origin, dest, product]) # origin, dest, product = map(lambda c: c.get_display_id(), [origin, dest, product]) # origin, dest, product = [c.get_display_id() if c else c for c in [origin, dest, product]] origin = origin.get_display_id() if origin else None dest = dest.get_display_id() if dest else "all" product = product.get_display_id() if product else "all" defaults = {"origin":"nausa", "dest":"aschn", "prod":"010101"} builds = models.get_all_builds("hs92", origin, dest, product, self.year, defaults, viz=["tree_map","rings","network","geo_map"]) for b in builds: exact.append({"value": b.question(), "name": b.url()}) return exact
def get_builds(self): origins, dests, products = [[None]] * 3 if len(self.countries): origins = self.countries[0] if len(self.countries) > 1: dests = self.countries[1] if len(self.hs) or len(self.sitc): products = sum([self.hs, self.sitc], []) combos = itertools.product([self.trade_flow], origins, dests, products) combos = [ c for c in combos if len(set(filter(None, c))) == len(filter(None, c)) ] def get_default(looking_for, have, trade_flow, classification, year): if looking_for == "dest": entity = getattr(db_data, "{}_models".format(classification)).Yod.query \ .filter_by(origin=have) \ .filter_by(year=available_years[classification][-1]) \ .order_by(desc(trade_flow+"_val")).limit(1).first() elif looking_for == "origin": entity = getattr(db_data, "{}_models".format(classification)).Yop.query \ .filter_by(product=have) \ .filter_by(year=available_years[classification][-1]) \ .order_by(desc(trade_flow+"_val")).limit(1).first() elif looking_for == "product": entity = getattr(db_data, "{}_models".format(classification)).Yop.query \ .filter_by(origin=have) \ .filter_by(year=available_years[classification][-1]) \ .order_by(desc(trade_flow+"_val")).limit(1).first() if entity: return getattr(entity, looking_for) exact = [] for combo in combos[:4]: trade_flow, origin, dest, product = combo origin = origin or get_default("origin", dest, "export", "hs92", self.year) dest = dest or get_default("dest", origin, "export", "hs92", self.year) product = product or get_default("product", origin, "export", "hs92", self.year) # raise Exception([origin, dest, product]) # origin, dest, product = map(lambda c: c.get_display_id(), [origin, dest, product]) # origin, dest, product = [c.get_display_id() if c else c for c in [origin, dest, product]] origin = origin.get_display_id() if origin else None dest = dest.get_display_id() if dest else "all" product = product.get_display_id() if product else "all" defaults = {"origin": "nausa", "dest": "aschn", "prod": "010101"} builds = models.get_all_builds( "hs92", origin, dest, product, self.year, defaults, viz=["tree_map", "rings", "network", "geo_map"]) for b in builds: exact.append({"value": b.question(), "name": b.url()}) return exact
def visualize(app_name, classification, trade_flow, origin_id, dest_id, prod_id, year=None): g.page_type = "visualize" g.page_sub_type = app_name if app_name == "scatter": year = [y for y in year if y >= 1980] elif trade_flow == "eci": year = [y for y in year if y >= 1964] '''sanitize input args''' redir = sanitize(app_name, classification, trade_flow, origin_id, dest_id, prod_id, year) if redir: flash(redir[0]) return redirect(redir[1]) '''get this build''' build = Build(app_name, classification, trade_flow, origin_id, dest_id, prod_id, year) if build.title() is None and build.question() is None: abort(404) '''get every possible build for sub nav''' origin, dest, prod = get_origin_dest_prod(origin_id, dest_id, prod_id, classification, year, trade_flow) all_builds = get_all_builds(classification, origin_id, dest_id, prod_id, year, { "origin": origin, "dest": dest, "prod": prod }) '''create the ui array for the current build''' ui = [] all_country = { "color": "#333333", "display_id": "all", "id": "all", "icon": "/static/img/icons/app/app_geo_map.png", "name": "All" } all_placed = False if build.trade_flow != "eci" and build.viz["slug"] not in ( "scatter", "geo_map") and build.origin != "show": if isinstance(build.origin, Country): origin_country = build.origin.serialize() else: origin_country = all_country all_placed = True ui.append({ "id": "origin", "name": gettext("Country"), "data": [origin_country], "url": url_for('attr.attrs', attr='country', lang=g.locale) }) dest_country = False prod_exists = isinstance(build.prod, (Sitc, Hs92, Hs96, Hs02, Hs07)) if isinstance(build.dest, Country): dest_country = build.dest.serialize() elif not all_placed and not prod_exists and build.viz["slug"] in ( "tree_map", "line", "stacked"): dest_country = all_country if dest_country: ui.append({ "id": "destination", "name": gettext("Country") if build.trade_flow == "eci" else gettext("Partner"), "data": [dest_country], "url": url_for('attr.attrs', attr='country', lang=g.locale) }) if prod_exists: product = { "id": "product", "name": gettext("Product"), "data": [build.prod.serialize()], "url": url_for('attr.attrs', attr=build.classification, lang=g.locale) } ui.append(product) if build.viz["slug"] == "scatter": trade_flow = { "id": "trade_flow", "name": gettext("Y Axis"), "current": build.trade_flow, "data": [{ "name": gettext("GDP"), "display_id": "gdp" }, { "name": gettext("GDPpc (constant '05 US$)"), "display_id": "gdp_pc_constant" }, { "name": gettext("GDPpc (current US$)"), "display_id": "gdp_pc_current" }, { "name": gettext("GDPpc PPP (constant '11)"), "display_id": "gdp_pc_constant_ppp" }, { "name": gettext("GDPpc PPP (current)"), "display_id": "gdp_pc_current_ppp" }] } else: trade_flow = { "id": "trade_flow", "name": gettext("Trade Flow"), "current": build.trade_flow, "data": [{ "name": gettext("Export"), "display_id": "export" }, { "name": gettext("Import"), "display_id": "import" }] } if build.trade_flow not in ("show", "eci"): ui.append(trade_flow) if build.trade_flow != "eci": ui.append({ "id": "classification", "name": gettext("Dataset"), "current": build.classification, "data": ["HS92", "HS96", "HS02", "HS07", "SITC"] }) years = set() for d in available_years: [ years.add(y) for y in available_years[d] if (build.trade_flow == "eci" and y >= 1964) or (build.viz["slug"] == "scatter" and y >= 1980) or ( build.viz["slug"] != "scatter" and build.trade_flow != "eci") ] if build.viz["slug"] in ("stacked", "line"): ui.append({ "id": "start_year", "name": gettext("Start Year"), "current": build.year[0], "data": years }) ui.append({ "id": "end_year", "name": gettext("End Year"), "current": build.year[-1], "data": years }) else: ui.append({ "id": "year", "name": gettext("Year"), "current": build.year[-1], "data": years }) builds_by_app = {} for b in all_builds: if b.viz["slug"] not in ("stacked", "line"): b.year = [b.year[-1]] b.year_str = Build.year_to_str(b, b.year) if not hasattr(builds_by_app, b.viz["slug"]): builds_by_app[b.viz["slug"]] = [] for b in all_builds: builds_by_app[b.viz["slug"]].append(b) page = "index_new" if request.args.get("beta") == "" else "index" return render_template("visualize/{}.html".format(page), current_build=build, build_ui=ui, all_builds=builds_by_app.values())
def visualize(app_name, classification, trade_flow, origin_id, dest_id, prod_id, year=None): g.page_type = "visualize" g.page_sub_type = app_name if app_name == "scatter": year = [y for y in year if y >= 1980] elif trade_flow == "eci": year = [y for y in year if y >= 1964] '''sanitize input args''' redir = sanitize(app_name, classification, trade_flow, origin_id, dest_id, prod_id, year) if redir: flash(redir[0]) return redirect(redir[1]) '''get this build''' build = Build(app_name, classification, trade_flow, origin_id, dest_id, prod_id, year) if build.title() is None and build.question() is None: abort(404) '''get every possible build for sub nav''' origin, dest, prod = get_origin_dest_prod(origin_id, dest_id, prod_id, classification, year, trade_flow) all_builds = get_all_builds(classification, origin_id, dest_id, prod_id, year, {"origin":origin, "dest":dest, "prod":prod}) '''create the ui array for the current build''' ui = [] all_country = { "color": "#333333", "display_id": "all", "id": "all", "icon": "/static/img/icons/app/app_geo_map.png", "name": "All" } all_placed = False if build.trade_flow != "eci" and build.viz["slug"] not in ("scatter", "geo_map") and build.origin != "show": if isinstance(build.origin, Country): origin_country = build.origin.serialize() else: origin_country = all_country all_placed = True ui.append({ "id": "origin", "name": gettext("Country"), "data": [origin_country], "url": url_for('attr.attrs', attr='country', lang=g.locale) }) dest_country = False prod_exists = isinstance(build.prod, (Sitc, Hs92, Hs96, Hs02, Hs07)) if isinstance(build.dest, Country): dest_country = build.dest.serialize() elif not all_placed and not prod_exists and build.viz["slug"] in ("tree_map", "line", "stacked"): dest_country = all_country if dest_country: ui.append({ "id": "destination", "name": gettext("Country") if build.trade_flow == "eci" else gettext("Partner"), "data": [dest_country], "url": url_for('attr.attrs', attr='country', lang=g.locale) }) if prod_exists: product = { "id": "product", "name": gettext("Product"), "data": [build.prod.serialize()], "url": url_for('attr.attrs', attr=build.classification, lang=g.locale) } ui.append(product) if build.viz["slug"] == "scatter": trade_flow = { "id": "trade_flow", "name": gettext("Y Axis"), "current": build.trade_flow, "data": [ {"name": gettext("GDP"), "display_id":"gdp"}, {"name": gettext("GDPpc (constant '05 US$)"), "display_id":"gdp_pc_constant"}, {"name": gettext("GDPpc (current US$)"), "display_id":"gdp_pc_current"}, {"name": gettext("GDPpc PPP (constant '11)"), "display_id":"gdp_pc_constant_ppp"}, {"name": gettext("GDPpc PPP (current)"), "display_id":"gdp_pc_current_ppp"} ] } else: trade_flow = { "id": "trade_flow", "name": gettext("Trade Flow"), "current": build.trade_flow, "data": [ {"name": gettext("Export"), "display_id":"export"}, {"name": gettext("Import"), "display_id":"import"} ] } if build.trade_flow not in ("show", "eci"): ui.append(trade_flow) if build.trade_flow != "eci": ui.append({ "id": "classification", "name": gettext("Dataset"), "current": build.classification, "data": ["HS92", "HS96", "HS02", "HS07", "SITC"] }) years = set() for d in available_years: [years.add(y) for y in available_years[d] if (build.trade_flow == "eci" and y >= 1964) or (build.viz["slug"] == "scatter" and y >= 1980) or (build.viz["slug"] != "scatter" and build.trade_flow != "eci")] if build.viz["slug"] in ("stacked", "line"): ui.append({ "id": "start_year", "name": gettext("Start Year"), "current": build.year[0], "data": years }) ui.append({ "id": "end_year", "name": gettext("End Year"), "current": build.year[-1], "data": years }) else: ui.append({ "id": "year", "name": gettext("Year"), "current": build.year[-1], "data": years }) builds_by_app = {} for b in all_builds: if b.viz["slug"] not in ("stacked", "line"): b.year = [b.year[-1]] b.year_str = Build.year_to_str(b, b.year) if not hasattr(builds_by_app, b.viz["slug"]): builds_by_app[b.viz["slug"]] = [] for b in all_builds: builds_by_app[b.viz["slug"]].append(b) page = "index_new" if request.args.get("beta") == "" else "index" return render_template("visualize/{}.html".format(page), current_build = build, build_ui = ui, all_builds = builds_by_app.values())