Beispiel #1
0
    def version_info():
        """
        :class:`flask:flask.Flask` route for retrieving version information about D-Tale

        :return: text/html version information
        """
        _, version = retrieve_meta_info_and_version("dtale")
        return str(version)
Beispiel #2
0
    def version_info():
        """
        Flask route for retrieving version information about D-Tale

        :return: text/html version information
        """
        _, version = retrieve_meta_info_and_version('dtale')
        return str(version)
Beispiel #3
0
def test_retrieve_meta_info_and_version():
    class MockDist(object):
        def __init__(self):
            self.version = "1.0.0"

        def _get_metadata(self, pkg_info):
            raise Exception()

    with mock.patch("pkg_resources.get_distribution",
                    mock.Mock(return_value=MockDist())):
        meta_info, version = retrieve_meta_info_and_version("foo")
        assert meta_info is None
        assert version == "1.0.0"
Beispiel #4
0
def base_render_template(template, **kwargs):
    """
    Overriden version of Flask.render_template which will also include vital instance information
     - settings
     - version
     - processes
    """
    port = get_port()
    curr_settings = SETTINGS.get(port, {})
    _, version = retrieve_meta_info_and_version('dtale')
    return render_template(template,
                           settings=json.dumps(curr_settings),
                           version=str(version),
                           processes=len(DATA),
                           **kwargs)
Beispiel #5
0
def build_app(url, host=None, reaper_on=True, hide_shutdown=False, github_fork=False):
    """
    Builds :class:`flask:flask.Flask` application encapsulating endpoints for D-Tale's front-end

    :return: :class:`flask:flask.Flask` application
    :rtype: :class:`dtale.app.DtaleFlask`
    """

    app = DtaleFlask('dtale', reaper_on=reaper_on, static_url_path='', url=url)
    app.config['SECRET_KEY'] = 'Dtale'
    app.config['HIDE_SHUTDOWN'] = hide_shutdown
    app.config['GITHUB_FORK'] = github_fork

    app.jinja_env.trim_blocks = True
    app.jinja_env.lstrip_blocks = True
    app.register_blueprint(dtale)

    compress = Compress()
    compress.init_app(app)

    _, version = retrieve_meta_info_and_version('dtale')
    template = dict(
        info={
            'title': 'D-Tale',
            'version': version,
            'description': 'Web Client for Visualizing Pandas Objects',
            'contact': {
                'name': 'Man Alpha Technology',
                'email': '*****@*****.**',
                'url': 'https://github.com/man-group/dtale'
            },
        },
        host=get_host(host),
        schemes=['http'],
    )
    try:
        from flasgger import Swagger  # flake8: NOQA
        Swagger(app, template=template)
    except ImportError:
        logger.debug('flasgger dependency not found, please install to enable feature')

    @app.route('/')
    @app.route('/dtale')
    @swag_from('swagger/dtale/root.yml')
    def root():
        """
        :class:`flask:flask.Flask` routes which redirect to dtale/main

        :return: 302 - flask.redirect('/dtale/main')
        """
        return redirect('/dtale/main/{}'.format(head_data_id()))

    @app.route('/favicon.ico')
    def favicon():
        """
        :class:`flask:flask.Flask` routes which returns favicon

        :return: image/png
        """
        return redirect(url_for('static', filename='images/favicon.ico'))

    @app.errorhandler(404)
    def page_not_found(e=None):
        """
        :class:`flask:flask.Flask` routes which returns favicon

        :param e: exception
        :return: text/html with exception information
        """
        logger.exception(e)
        return render_template('dtale/errors/404.html', page='', error=e,
                               stacktrace=str(traceback.format_exc())), 404

    @app.errorhandler(500)
    def internal_server_error(e=None):
        """
        :class:`flask:flask.Flask` route which returns favicon

        :param e: exception
        :return: text/html with exception information
        """
        logger.exception(e)
        return render_template('dtale/errors/500.html', page='', error=e,
                               stacktrace=str(traceback.format_exc())), 500

    def shutdown_server():
        global ACTIVE_HOST, ACTIVE_PORT
        """
        This function that checks if flask.request.environ['werkzeug.server.shutdown'] exists and
        if so, executes that function
        """
        logger.info('Executing shutdown...')
        func = request.environ.get('werkzeug.server.shutdown')
        if func is None:
            raise RuntimeError('Not running with the Werkzeug Server')
        func()
        cleanup()
        ACTIVE_PORT = None
        ACTIVE_HOST = None

    @app.route('/shutdown')
    @swag_from('swagger/dtale/shutdown.yml')
    def shutdown():
        """
        :class:`flask:flask.Flask` route for initiating server shutdown

        :return: text/html with server shutdown message
        """
        app.clear_reaper()
        shutdown_server()
        return 'Server shutting down...'

    @app.before_request
    def before_request():
        """
        Logic executed before each :attr:`flask:flask.request`

        :return: text/html with server shutdown message
        """
        app.build_reaper()

    @app.route('/site-map')
    @swag_from('swagger/dtale/site-map.yml')
    def site_map():
        """
        :class:`flask:flask.Flask` route listing all available flask endpoints

        :return: JSON of all flask enpoints [
            [endpoint1, function path1],
            ...,
            [endpointN, function pathN]
        ]
        """

        def has_no_empty_params(rule):
            defaults = rule.defaults or ()
            arguments = rule.arguments or ()
            return len(defaults) >= len(arguments)

        links = []
        for rule in app.url_map.iter_rules():
            # Filter out rules we can't navigate to in a browser
            # and rules that require parameters
            if "GET" in rule.methods and has_no_empty_params(rule):
                url = url_for(rule.endpoint, **(rule.defaults or {}))
                links.append((url, rule.endpoint))
        return jsonify(links)

    @app.route('/version-info')
    @swag_from('swagger/dtale/version-info.yml')
    def version_info():
        """
        :class:`flask:flask.Flask` route for retrieving version information about D-Tale

        :return: text/html version information
        """
        _, version = retrieve_meta_info_and_version('dtale')
        return str(version)

    @app.route('/health')
    @swag_from('swagger/dtale/health.yml')
    def health_check():
        """
        :class:`flask:flask.Flask` route for checking if D-Tale is up and running

        :return: text/html 'ok'
        """
        return 'ok'

    return app