import json import os from pathlib import Path from urllib.parse import quote_plus import opendataschema import flask import jinja2 import requests import tableschema from cachetools.func import ttl_cache # Let this import after app initialisation from . import config @ttl_cache(maxsize=20*1024, ttl=5*60) def download_with_cache(url): return requests.get(url) @ttl_cache(maxsize=20*1024, ttl=5*60) def schema_from_url(url): return tableschema.Schema(url) # load config.json and catalog_schema_toml ui_config = json.load(config.UI_CONFIG_FILE.open('rt', encoding='utf-8')) if config.UI_CONFIG_FILE else [] # super ugly way to access catalog_toml url # TODO: improve it schema_catalog_url = ui_config['sections'][0]['catalog'] table_schema_catalog = opendataschema.SchemaCatalog(schema_catalog_url, download_func=download_with_cache) # Flask things app = flask.Flask(__name__) app.secret_key = config.SECRET_KEY # Jinja2 url_quote_plus custom filter # https://stackoverflow.com/questions/12288454/how-to-import-custom-jinja2-filters-from-another-file-and-using-flask blueprint = flask.Blueprint('filters', __name__) @jinja2.contextfilter @blueprint.app_template_filter() def urlencode(context, value): return quote_plus(value) # Keep this import after app initialisation (to avoid cyclic imports) from . import views # isort:skip