<commit_before># Import flask
import os
import argparse
from flask import Flask, render_template, request, Response, jsonify
import werkzeug.serving
import subprocess
import gevent
from gevent import monkey
from socketio import socketio_manage
from socketio.server import SocketIOServer
import dashboard as Dashboard
import webbrowser
# import zmq
import socket
from PsiTurkConfig import PsiTurkConfig
import signal
import urllib2

config = PsiTurkConfig()

monkey.patch_all()


def launch_browser(port):
    launchurl = "http://{host}:{port}/dashboard".format(
        host=config.get("Server Parameters", "host"), 
        port=port)
    webbrowser.open(launchurl, new=1, autoraise=True)

def is_port_available(port):
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    try:
        s.connect(('127.0.0.1', port))
        s.shutdown(1)
        return 0
    except:
        return 1

# Pass messages between psiturk and dashboard via socket
# pubsub_socket_port = int(sys.argv[1])
# print(pubsub_socket_port)
# context = zmq.Context()
# pubsub_socket = context.socket(zmq.PUB)
# pubsub_socket.connect("tcp://127.0.0.1:" + str(pubsub_socket_port))

def find_open_port():
    open_port = next(port for port in range(5000, 10000) if is_port_available(port))
    return(open_port)


app = Flask("Psiturk_Dashboard", 
            template_folder=os.path.join(os.path.dirname(__file__), "templates_dashboard"), 
            static_folder=os.path.join(os.path.dirname(__file__), "static_dashboard"))

# Routes for handling dashboard activity
@app.route('/dashboard', methods=['GET'])
def dashboard():
    """
    Serves dashboard.
    """
    return render_template('dashboard.html')

@app.route('/dashboard_model', methods=['GET', 'POST'])
def dashbaord_model():
    """
    Sync for dashboard model.
    """
    if request.method == 'GET':
        return jsonify(config.get_serialized())

    if request.method == 'POST':
        config_model = request.json
        config.set_serialized(config_model)

    return render_template('dashboard.html')

@app.route('/at_a_glance_model', methods=['GET'])
def at_a_glance_model():
    """
    Sync for dashboard at-a-glance pane.
    """
    services = Dashboard.MTurkServices(config)

    if request.method == 'GET':
        return services.get_summary()

@app.route('/verify_aws_login', methods=['POST'])
def verify_aws():
    """
    """
    services = Dashboard.MTurkServices(config)
    key_id = request.json['aws_access_key_id']<commit_msg>return False, instead of fail, when aws is inaccessible. reduces "500" errors and hanging ajax calls.<commit_after># Import flask
import os
import argparse
from flask import Flask, render_template, request, Response, jsonify
import werkzeug.serving
import subprocess
import gevent
from gevent import monkey
from socketio import socketio_manage
from socketio.server import SocketIOServer
import dashboard as Dashboard
import webbrowser
# import zmq
import socket
from PsiTurkConfig import PsiTurkConfig
import signal
import urllib2

config = PsiTurkConfig()

monkey.patch_all()


def launch_browser(port):
    launchurl = "http://{host}:{port}/dashboard".format(
        host=config.get("Server Parameters", "host"), 
        port=port)
    webbrowser.open(launchurl, new=1, autoraise=True)

def is_port_available(port):
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    try:
        s.connect(('127.0.0.1', port))
        s.shutdown(1)
        return 0
    except:
        return 1

# Pass messages between psiturk and dashboard via socket
# pubsub_socket_port = int(sys.argv[1])
# print(pubsub_socket_port)
# context = zmq.Context()
# pubsub_socket = context.socket(zmq.PUB)
# pubsub_socket.connect("tcp://127.0.0.1:" + str(pubsub_socket_port))

def find_open_port():
    open_port = next(port for port in range(5000, 10000) if is_port_available(port))
    return(open_port)


app = Flask("Psiturk_Dashboard", 
            template_folder=os.path.join(os.path.dirname(__file__), "templates_dashboard"), 
            static_folder=os.path.join(os.path.dirname(__file__), "static_dashboard"))

# Routes for handling dashboard activity
@app.route('/dashboard', methods=['GET'])
def dashboard():
    """
    Serves dashboard.
    """
    return render_template('dashboard.html')

@app.route('/dashboard_model', methods=['GET', 'POST'])
def dashbaord_model():
    """
    Sync for dashboard model.
    """
    if request.method == 'GET':
        return jsonify(config.get_serialized())

    if request.method == 'POST':
        config_model = request.json
        config.set_serialized(config_model)

    return render_template('dashboard.html')

@app.route('/at_a_glance_model', methods=['GET'])
def at_a_glance_model():
    """
    Sync for dashboard at-a-glance pane.
    """
    services = Dashboard.MTurkServices(config)

    if request.method == 'GET':
        summary = services.get_summary()
        if summary:
            return summary
        else:
            return "unable to access aws"

@app.route('/verify_aws_login', methods=['POST'])
def verify_aws():
    """
    """
    services = Dashboard.MTurkServices(config)
    key_id = request.json['aws_access_key_id']
{'fraction_non_alphanumeric': 0.12287808641975309, 'fraction_numerical': 0.01099537037037037, 'mean_word_length': 3.9333967649857278, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 4, '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': '6172090', 'n_tokens_mistral': 1702, 'n_tokens_neox': 1632, 'n_words': 370}