<commit_before>import flask
import json
import plotly
from flask import Flask, url_for, send_from_directory
from flask.ext.cors import CORS
from dependency_resolver import Resolver


class Dash(object):
    def __init__(self, name=None, url_namespace='', server=None):

        self.layout = None

        # Resolve site-packages location by using plotly as canonical
        # dependency.
        self.resolver = Resolver(plotly, 'plotly')

        self.react_map = {}

        if server is not None:
            self.server = server
        else:
            self.server = Flask(name)

        # The name and port number of the server.
        # Required for subdomain support (e.g.: 'myapp.dev:5000')
        self.server.config['SERVER_NAME'] = 'localhost:8050'

        CORS(self.server)  # TODO: lock this down to dev node server port

        self.server.add_url_rule(
            '{}/'.format(url_namespace),
            endpoint='{}_{}'.format(url_namespace, 'index'),
            view_func=self.index)

        # TODO - Rename "initialize". Perhaps just "GET /components"
        self.server.add_url_rule(
            '{}/initialize'.format(url_namespace),
            view_func=self.initialize,
            endpoint='{}_{}'.format(url_namespace, 'initialize'))

        self.server.add_url_rule(

        # TODO - A different name for "interceptor".
        # TODO - Should the "interceptor"'s API be keyed by component ID?
        # For example: POST dash.com/components/my-id/update
            '{}/interceptor'.format(url_namespace),
            view_func=self.interceptor,
            endpoint='{}_{}'.format(url_namespace, 'interceptor'),
            methods=['POST'])

        self.server.add_url_rule(
            '{}/js/component-suites/<path:path>'.format(url_namespace),
            endpoint='{}_{}'.format(url_namespace, 'js'),
            view_func=self.component_suites)

        # Serve up the main dash bundle with the treeRenderer
        with self.server.app_context():
            url_for('static', filename='bundle.js')

    def component_suites(self, path):
        name = self.resolver.resolve_dependency_name(path)
        return send_from_directory(self.resolver.site_packages_path, name)

    def index(self):
        return flask.render_template(
            'index.html',
            component_suites=self.component_suites
        )

    def initialize(self):
        return flask.jsonify(json.loads(json.dumps(self.layout,
                             cls=plotly.utils.PlotlyJSONEncoder)))

    def interceptor(self):
        body = json.loads(flask.request.get_data())
        target = body['target']<commit_msg>serve html string instead of html file<commit_after>import flask
import json
import plotly
from flask import Flask, url_for, send_from_directory
from flask.ext.cors import CORS
from dependency_resolver import Resolver


class Dash(object):
    def __init__(self, name=None, url_namespace='', server=None):

        self.layout = None

        # Resolve site-packages location by using plotly as canonical
        # dependency.
        self.resolver = Resolver(plotly, 'plotly')

        self.react_map = {}

        if server is not None:
            self.server = server
        else:
            self.server = Flask(name)

        # The name and port number of the server.
        # Required for subdomain support (e.g.: 'myapp.dev:5000')
        self.server.config['SERVER_NAME'] = 'localhost:8050'

        CORS(self.server)  # TODO: lock this down to dev node server port

        self.server.add_url_rule(
            '{}/'.format(url_namespace),
            endpoint='{}_{}'.format(url_namespace, 'index'),
            view_func=self.index)

        # TODO - Rename "initialize". Perhaps just "GET /components"
        self.server.add_url_rule(
            '{}/initialize'.format(url_namespace),
            view_func=self.initialize,
            endpoint='{}_{}'.format(url_namespace, 'initialize'))

        self.server.add_url_rule(

        # TODO - A different name for "interceptor".
        # TODO - Should the "interceptor"'s API be keyed by component ID?
        # For example: POST dash.com/components/my-id/update
            '{}/interceptor'.format(url_namespace),
            view_func=self.interceptor,
            endpoint='{}_{}'.format(url_namespace, 'interceptor'),
            methods=['POST'])

        self.server.add_url_rule(
            '{}/js/component-suites/<path:path>'.format(url_namespace),
            endpoint='{}_{}'.format(url_namespace, 'js'),
            view_func=self.component_suites)

        # Serve up the main dash bundle with the treeRenderer
        with self.server.app_context():
            url_for('static', filename='bundle.js')

    def component_suites(self, path):
        name = self.resolver.resolve_dependency_name(path)
        return send_from_directory(self.resolver.site_packages_path, name)

    def index(self):
        scripts = ', '.join([
            '"{}/js/component-suites/{}/bundle.js"'.format(
                self.url_namespace, suite
            )
            for suite in self.component_suites
        ])
        return ('''
        <!DOCTYPE html>
        <html>
            <head>
                <meta charset="UTF-8" />
                <script
                    src="https://cdn.rawgit.com/ded/script.js/master/dist/script.min.js"
                    type="text/javascript"
                ></script>
            </head>
            <body>
                <div id="react-entry-point"></div>
            </body>

            <footer>

                <!-- TODO: Move this logic into a bundle? -->
                <script type="text/javascript">
                    $script([
                        "https://unpkg.com/react@15/dist/react.js",
                        "https://unpkg.com/react-dom@15/dist/react-dom.js"
                    ], function() {{

                        $script([{}], function() {{

                            $script("{}/js/component-suites/dash_renderer/bundle.js");

                        }});

                    }});
                </script>

            </footer>
        </html>
        '''.format(scripts, self.url_namespace))

    def initialize(self):
        return flask.jsonify(json.loads(json.dumps(self.layout,
                             cls=plotly.utils.PlotlyJSONEncoder)))

    def interceptor(self):
        body = json.loads(flask.request.get_data())
        target = body['target']
{'fraction_non_alphanumeric': 0.1213292117465224, 'fraction_numerical': 0.0032457496136012367, 'mean_word_length': 2.0224194301728162, 'pattern_counts': {'":': 0, '<': 22, '<?xml version=': 0, '>': 22, 'https://': 3, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1944164', 'n_tokens_mistral': 1858, 'n_tokens_neox': 1773, 'n_words': 376}