def register_blueprints(app): from .handlers import front, course, admin, live, ws app.register_blueprint(front) app.register_blueprint(course) app.register_blueprint(admin) app.register_blueprint(live) sockets = Sockets(app) sockets.register_blueprint(ws)
from flask import Flask from flask_sockets import Sockets import pygame , sys from pygame.locals import * import math from random import randint import time import striker app = Flask(__name__) sockets = Sockets(app) @sockets.route('/accelerometer') def echo_socket(ws): global now while True: message = ws.receive() # print(message) player , data = message.split(":") player=int(player) y_data,x_data=map(int,data.split(',')) # print(player,x_data,y_data) # print(y_data)
@html.route("/") def hello(): return "Try and add a table id to connect to this server!" @html.route("/create-cambio-table", methods=["POST"]) def create_new_table(): pass @ws.route("/echo") def echo_socket(socket): while not socket.closed: message = socket.receive() socket.send(message) app = Flask(__name__) sockets = Sockets(app) app.register_blueprint(html, url_prefix=r"/") sockets.register_blueprint(ws, url_prefix=r"/") if __name__ == "__main__": from gevent import pywsgi from geventwebsocket.handler import WebSocketHandler server = pywsgi.WSGIServer(("", 5000), app, handler_class=WebSocketHandler) server.serve_forever()
def create_app(): app = Flask(__name__) @app.route('/') def index(): return redirect('/graphql') # GraphQL endpoints ---------------------------------------------- app.add_url_rule('/graphql', view_func=load_auth_info( GraphQLView.as_view('graphql', schema=schema, graphiql=True))) # Optional, for adding batch query support (used in Apollo-Client) app.add_url_rule('/graphql/batch', view_func=load_auth_info( GraphQLView.as_view('graphql-batch', schema=schema, batch=True))) # RESTful methods for authentication ----------------------------- # TODO: provide this via GraphQL API # @app.errorhandler(401) # def handle_401(error): # return Response(str(error), 401, { # 'WWWAuthenticate': 'Basic realm="Login Required"', # }) # Websockets ----------------------------------------------------- sockets = Sockets(app) app.app_protocol = lambda environ_path_info: 'graphql-ws' class SubscriptionServer(GeventSubscriptionServer): def on_connect(self, connection_context, payload): logger.debug('SubscriptionServer.on_connect(%s, %s)', repr(connection_context), repr(payload)) # TODO: is there a better way to pass context down, without # having to inject stuff into the connection context class?? connection_context.auth_context = get_socket_context(payload) def on_message(self, connection_context, message): logger.debug('On Message: %s', repr((connection_context, message))) if message is None: self._dispose_subscription() return super().on_message(connection_context, message) def on_start(self, connection_context, op_id, params): from rx import Observable from graphql_ws.gevent import SubscriptionObserver try: execution_result = self.execute( connection_context.request_context, params) assert isinstance( execution_result, Observable), \ "A subscription must return an observable" observer = SubscriptionObserver(connection_context, op_id, self.send_execution_result, self.send_error, self.on_close) self._subscription = execution_result.subscribe(observer) except Exception as e: self.send_error(connection_context, op_id, str(e)) def unsubscribe(self, *a, **kw): self._dispose_subscription() super().unsubscribe(*a, **kw) def _dispose_subscription(self): logger.debug('Disposing of subscription') self._subscription.dispose() def get_graphql_params(self, connection_context, payload): _params = super().get_graphql_params(connection_context, payload) return { **_params, 'context_value': connection_context.auth_context, } subscription_server = SubscriptionServer(schema) @sockets.route('/subscriptions') def echo_socket(ws): # context = get_socket_context(ws) # logger.debug('Socket: handling %s (%s)', repr(ws), repr(context)) subscription_server.handle(ws) return [] # Add CORS support ----------------------------------------------- CORS(app) return app
class WebAPI(object): def __init__(self, oauth_config=None): self.app = Flask(__name__) self.app.response_class = IppResponse self.app.before_first_request(self.torun) self.sockets = Sockets(self.app) self.socks = dict() self.port = 0 # authentication/authorisation self._oauth_config = oauth_config self._authorize = self.default_authorize self._authenticate = self.default_authenticate self.add_routes(self, basepath='') # Enable ProxyFix middleware if required if _config.get('fix_proxy') == 'enabled': self.app.wsgi_app = ProxyFix(self.app.wsgi_app) def add_routes(self, routesObject, basepath): assert not basepath.endswith('/'), "basepath must not end with a slash" def dummy(f): @wraps(f) def inner(*args, **kwargs): return f(*args, **kwargs) return inner def getbases(cls): bases = list(cls.__bases__) for x in cls.__bases__: bases += getbases(x) return bases for cls in [routesObject.__class__, ] + getbases(routesObject.__class__): for attr in cls.__dict__.keys(): routesMethod = getattr(routesObject, attr) if callable(routesMethod): endpoint = "{}_{}".format(basepath.replace('/', '_'), routesMethod.__name__) if hasattr(routesMethod, 'app_methods') and routesMethod.app_methods is not None: methods = routesMethod.app_methods else: methods = ["GET", "HEAD"] if hasattr(routesMethod, 'app_headers') and routesMethod.app_headers is not None: headers = routesMethod.app_headers else: headers = [] if hasattr(routesMethod, "secure_route"): self.app.route( basepath + routesMethod.secure_route, endpoint=endpoint, methods=methods + ["OPTIONS"])( crossdomain( origin=routesMethod.app_origin, methods=methods, headers=headers + ['Content-Type', 'Authorization', 'token', ])( returns_requires_auth(routesMethod))) elif hasattr(routesMethod, "response_route"): self.app.route( basepath + routesMethod.response_route, endpoint=endpoint, methods=["GET", "POST", "HEAD", "OPTIONS"])( crossdomain( origin='*', methods=['GET', 'POST', 'HEAD'], headers=['Content-Type', 'Authorization', ])( returns_response(routesMethod))) elif hasattr(routesMethod, "app_route"): if routesMethod.app_auto_json: self.app.route( basepath + routesMethod.app_route, endpoint=endpoint, methods=methods + ["OPTIONS", ])( crossdomain( origin=routesMethod.app_origin, methods=methods, headers=headers + ['Content-Type', 'Authorization', ])( returns_json(routesMethod))) else: self.app.route( basepath + routesMethod.app_route, endpoint=endpoint, methods=methods + ["OPTIONS", ])( crossdomain( origin=routesMethod.app_origin, methods=methods, headers=headers + ['Content-Type', 'Authorization', ])( dummy(routesMethod))) elif hasattr(routesMethod, "app_file_route"): self.app.route( basepath + routesMethod.app_file_route, endpoint=endpoint, methods=methods + ["OPTIONS"])( crossdomain( origin='*', methods=methods, headers=headers + ['Content-Type', 'Authorization', ])( returns_file(routesMethod))) elif hasattr(routesMethod, "app_resource_route"): f = crossdomain(origin='*', methods=methods, headers=headers + ['Content-Type', 'Authorization', 'api-key', ])( returns_json(obj_path_access(routesMethod))) self.app.route( basepath + routesMethod.app_resource_route, methods=methods + ["OPTIONS", ], endpoint=endpoint)(f) f.__name__ = endpoint + '_path' self.app.route( basepath + routesMethod.app_resource_route + '<path:path>/', methods=methods + ["OPTIONS", ], endpoint=f.__name__)(f) elif hasattr(routesMethod, "socket_path"): websocket_opened = getattr(routesObject, "on_websocket_connect", None) if websocket_opened is None: self.sockets.route( basepath + routesMethod.socket_path, endpoint=endpoint)( self.handle_sock( expects_json(routesMethod), self.socks)) else: self.sockets.route( basepath + routesMethod.socket_path, endpoint=endpoint)( websocket_opened( expects_json(routesMethod))) elif hasattr(routesMethod, "errorhandler_args"): if routesMethod.errorhandler_args: self.app.errorhandler( *routesMethod.errorhandler_args, **routesMethod.errorhandler_kwargs)( crossdomain( origin='*', methods=["GET", "POST", "PUT", "DELETE", "OPTIONS", "HEAD"])( dummy(routesMethod))) else: for n in range(400, 600): try: # Apply errorhandlers for all known error codes self.app.errorhandler(n)(crossdomain( origin='*', methods=["GET", "POST", "PUT", "DELETE", "OPTIONS", "HEAD"])( dummy(routesMethod))) except KeyError: # Some error codes aren't valid pass @errorhandler() def error(self, e): if request.method == 'HEAD': if isinstance(e, HTTPException): return IppResponse('', status=e.code) else: return IppResponse('', 500) bm = request.accept_mimetypes.best_match(['application/json', 'text/html']) if bm == 'text/html': if isinstance(e, HTTPException): if e.code == 400: (exceptionType, exceptionParam, trace) = sys.exc_info() return IppResponse(highlight( '\n'.join(traceback.format_exception(exceptionType, exceptionParam, trace)), PythonTracebackLexer(), HtmlFormatter(linenos='table', full=True, title="{}: {}".format(e.code, e.description))), status=e.code, mimetype='text/html') return e.get_response() (exceptionType, exceptionParam, trace) = sys.exc_info() return IppResponse(highlight( '\n'.join(traceback.format_exception(exceptionType, exceptionParam, trace)), PythonTracebackLexer(), HtmlFormatter(linenos='table', full=True, title='500: Internal Exception')), status=500, mimetype='text/html') else: (exceptionType, exceptionParam, trace) = sys.exc_info() if isinstance(e, HTTPException): response = { 'code': e.code, 'error': e.description, 'debug': str({ 'traceback': [str(x) for x in traceback.extract_tb(trace)], 'exception': [str(x) for x in traceback.format_exception_only(exceptionType, exceptionParam)] }) } return IppResponse(json.dumps(response), status=e.code, mimetype='application/json') if isinstance(e, AuthlibHTTPError): response = { 'code': e.status_code, 'error': e.description, 'debug': str({ 'traceback': [str(x) for x in traceback.extract_tb(trace)], 'exception': [str(x) for x in traceback.format_exception_only(exceptionType, exceptionParam)] }) } return IppResponse(json.dumps(response), status=e.status_code, mimetype='application/json') if isinstance(e, AuthlibBaseError): response = { 'code': 400, 'error': e.description, 'debug': str({ 'traceback': [str(x) for x in traceback.extract_tb(trace)], 'exception': [str(x) for x in traceback.format_exception_only(exceptionType, exceptionParam)] }) } return IppResponse(json.dumps(response), status=400, mimetype='application/json') response = { 'code': 500, 'error': 'Internal Error', 'debug': str({ 'traceback': [str(x) for x in traceback.extract_tb(trace)], 'exception': [str(x) for x in traceback.format_exception_only(exceptionType, exceptionParam)] }) } return IppResponse(json.dumps(response), status=500, mimetype='application/json') def torun(self): # pragma: no cover pass def stop(self): # pragma: no cover pass def handle_sock(self, func, socks): @wraps(func) def inner_func(ws, **kwds): sock_uuid = uuid.uuid4() socks[sock_uuid] = ws print("Opening Websocket {} at path /, Receiving ...".format(sock_uuid)) while True: try: message = ws.receive() except Exception: message = None if message is not None: func(ws, message, **kwds) continue else: print("Websocket {} closed".format(sock_uuid)) del socks[sock_uuid] break return inner_func def default_authorize(self, token): if self._oauth_config is not None: # Ensure the user is permitted to use function print('authorizing: {}'.format(token)) loginserver = self._oauth_config['loginserver'] proxies = self._oauth_config['proxies'] whitelist = self._oauth_config['access_whitelist'] result = proxied_request( uri="{}/check-token".format(loginserver), headers={'token': token}, proxies=proxies) json_payload = json.loads(result[1]) return json_payload['userid'] in whitelist else: return True def default_authenticate(self, token): if self._oauth_config is not None: # Validate the token that the webapp sends loginserver = self._oauth_config['loginserver'] proxies = self._oauth_config['proxies'] print('authenticating: {}'.format(token)) if token is None: return False result = proxied_request( uri="{}/check-token".format(loginserver), headers={'token': token}, proxies=proxies) print('token result code: {}'.format(result[0].code)) print('result payload: {}'.format(result[1])) return result[0].code == 200 and json.loads(result[1])['token'] == token else: return True def authorize(self, f): self._authorize = f return f def authenticate(self, f): self._authenticate = f return f
class RouteManager: def __init__(self): # Set up the app and the database self.app = Flask( __name__, template_folder=TEMPLATES_PATH, static_folder=STATIC_PATH, static_url_path="/static", ) self.sockets = Sockets(self.app) self.db = RethinkDB() self.log = logging.getLogger() self.app.secret_key = os.environ.get("WEBPAGE_SECRET_KEY", "super_secret") self.app.config["SERVER_NAME"] = os.environ.get( "SERVER_NAME", "pythondiscord.local:8080") self.app.config["PREFERRED_URL_SCHEME"] = PREFERRED_URL_SCHEME self.app.before_request(self.db.before_request) self.app.teardown_request(self.db.teardown_request) # Load the oauth blueprint self.oauth_backend = OauthBackend(self) self.oauth_blueprint = make_discord_blueprint( DISCORD_OAUTH_ID, DISCORD_OAUTH_SECRET, DISCORD_OAUTH_SCOPE, '/', login_url=DISCORD_OAUTH_REDIRECT, authorized_url=DISCORD_OAUTH_AUTHORIZED, backend=self.oauth_backend) self.log.debug(f"Loading Blueprint: {self.oauth_blueprint.name}") self.app.register_blueprint(self.oauth_blueprint) self.log.debug("") # Load the main blueprint self.main_blueprint = Blueprint("main", __name__) self.log.debug(f"Loading Blueprint: {self.main_blueprint.name}") self.load_views(self.main_blueprint, "pysite/views/main") self.load_views(self.main_blueprint, "pysite/views/error_handlers") self.app.register_blueprint(self.main_blueprint) self.log.debug("") # Load the subdomains self.subdomains = ['api', 'staff'] for sub in self.subdomains: sub_blueprint = Blueprint(sub, __name__, subdomain=sub) self.log.debug(f"Loading Blueprint: {sub_blueprint.name}") self.load_views(sub_blueprint, f"pysite/views/{sub}") try: self.app.register_blueprint(sub_blueprint) except Exception: logging.getLogger(__name__).exception( f"Failed to register blueprint for subdomain: {sub}") # Load the websockets self.ws_blueprint = Blueprint("ws", __name__) self.log.debug("Loading websocket routes...") self.load_views(self.ws_blueprint, "pysite/views/ws") self.sockets.register_blueprint(self.ws_blueprint, url_prefix="/ws") self.app.before_request( self.https_fixing_hook) # Try to fix HTTPS issues def https_fixing_hook(self): """ Attempt to fix HTTPS issues by modifying the request context stack """ if _request_ctx_stack is not None: reqctx = _request_ctx_stack.top reqctx.url_adapter.url_scheme = PREFERRED_URL_SCHEME def run(self): from gevent.pywsgi import WSGIServer from geventwebsocket.handler import WebSocketHandler server = WSGIServer( ("0.0.0.0", int(os.environ.get("WEBPAGE_PORT", 8080))), # noqa: B104, S104 self.app, handler_class=WebSocketHandler) server.serve_forever() def load_views(self, blueprint, location="pysite/views"): for filename in os.listdir(location): if os.path.isdir(f"{location}/{filename}"): # Recurse if it's a directory; load ALL the views! self.load_views(blueprint, location=f"{location}/{filename}") continue if filename.endswith( ".py") and not filename.startswith("__init__"): module = importlib.import_module( f"{location}/{filename}".replace("/", ".")[:-3]) for cls_name, cls in inspect.getmembers(module): if (inspect.isclass(cls) and cls is not BaseView and cls is not ErrorView and cls is not RouteView and cls is not APIView and cls is not WS and (BaseView in cls.__mro__ or WS in cls.__mro__)): cls.setup(self, blueprint) self.log.debug( f">> View loaded: {cls.name: <15} ({module.__name__}.{cls_name})" )
def instantiate_app_with_views(context, app_path_prefix): app = Flask( "dagster-ui", static_url_path=app_path_prefix, static_folder=os.path.join(os.path.dirname(__file__), "./webapp/build"), ) schema = create_schema() subscription_server = DagsterSubscriptionServer(schema=schema) # Websocket routes sockets = Sockets(app) sockets.add_url_rule( "{}/graphql".format(app_path_prefix), "graphql", dagster_graphql_subscription_view(subscription_server, context), ) # HTTP routes bp = Blueprint("routes", __name__, url_prefix=app_path_prefix) bp.add_url_rule( "/graphiql", "graphiql", lambda: redirect("{}/graphql".format(app_path_prefix), 301)) bp.add_url_rule( "/graphql", "graphql", DagsterGraphQLView.as_view( "graphql", schema=schema, graphiql=True, graphiql_template=PLAYGROUND_TEMPLATE.replace( "APP_PATH_PREFIX", app_path_prefix), executor=Executor(), context=context, ), ) bp.add_url_rule( # should match the `build_local_download_url` "/download/<string:run_id>/<string:step_key>/<string:file_type>", "download_view", download_log_view(context), ) bp.add_url_rule( "/download_debug/<string:run_id>", "download_dump_view", download_dump_view(context), ) # these routes are specifically for the Dagit UI and are not part of the graphql # API that we want other people to consume, so they're separate for now. # Also grabbing the magic global request args dict so that notebook_view is testable bp.add_url_rule("/dagit/notebook", "notebook", lambda: notebook_view(request.args)) bp.add_url_rule("/dagit_info", "sanity_view", info_view) index_path = os.path.join(os.path.dirname(__file__), "./webapp/build/index.html") def index_view(_path): try: with open(index_path) as f: return (f.read().replace( 'href="/', 'href="{}/'.format(app_path_prefix)).replace( 'src="/', 'src="{}/'.format(app_path_prefix)).replace( '<meta name="dagit-path-prefix"', '<meta name="dagit-path-prefix" content="{}"'. format(app_path_prefix), )) except FileNotFoundError: raise Exception( """Can't find webapp files. Probably webapp isn't built. If you are using dagit, then probably it's a corrupted installation or a bug. However, if you are developing dagit locally, your problem can be fixed as follows: cd ./python_modules/ make rebuild_dagit""") app.app_protocol = lambda environ_path_info: "graphql-ws" app.register_blueprint(bp) app.register_error_handler(404, index_view) # if the user asked for a path prefix, handle the naked domain just in case they are not # filtering inbound traffic elsewhere and redirect to the path prefix. if app_path_prefix: app.add_url_rule("/", "force-path-prefix", lambda: redirect(app_path_prefix, 301)) CORS(app) return app
def __init__(self, dims): self.dims = dims import numpy as np self.np = np from flask import Flask, render_template, request from flask_sockets import Sockets import json from random import randint # from itertools import cycle # from matplotlib import cm # num_cols = 512 # Pick a colour map from here: https://matplotlib.org/users/colormaps.html # rainbow = cm.get_cmap('PuBu', num_cols) # self.sky_colours = [[int(c * 256) for c in rainbow(i)[:-1]] for i in range(num_cols)] # self.sky_colours = cycle(self.sky_colours + self.sky_colours[::-1]) app = Flask('UnityGame') sockets = Sockets(app) self.things = [{ 'type': 'cloud', 'xpos': 90, 'ypos': 1, 'frame': 0, 'frame_rate': 0, 'velocity': 0.2, }, { 'type': 'gull', 'xpos': 90, 'ypos': 1, 'frame': 0, 'frame_rate': 0.2, 'velocity': 1, }] self.things_templates = { 'cloud': { 'colours': [[0xd8, 0xad, 0xde], [253, 245, 251]], 'template': np.array([[ # gratuitously stolen from: https://cdn.dribbble.com/users/1113/screenshots/150244/pixelcloud-dribbble.png [0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 1, 1, 2, 2, 1, 1, 0, 0, 0, 0], [0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 2, 1, 0, 0, 0], [0, 0, 1, 1, 2, 2, 1, 1, 2, 2, 2, 2, 1, 0, 0, 0], [0, 1, 1, 2, 2, 2, 2, 1, 2, 2, 2, 2, 1, 1, 0, 0], [0, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1, 1, 0], [1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1], [1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1], [1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1, 1], [0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0], ]]) }, 'gull': { 'colours': [[77, 77, 77], [253, 245, 251]], 'template': np.array([[ [0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0], [0, 1, 1, 0, 1, 1, 0], [1, 0, 0, 1, 0, 0, 1], ], [ [0, 0, 0, 0, 0, 0, 0], [0, 1, 0, 0, 0, 1, 0], [1, 0, 1, 0, 1, 0, 1], [0, 0, 0, 1, 0, 0, 0], ]]) }, 'boat': { 'template': [[0, 0, 0, 0, 0, 0, 0, 2], [0, 0, 0, 0, 0, 0, 2, 3], [0, 1, 1, 1, 0, 2, 0, 3], [1, 1, 1, 1, 1, 1, 0, 3], [0, 1, 1, 1, 1, 0, 0, 3]] } } self.current_players = {} game_x_min = 0 game_x_max = 100 self.game_y_max = 14 boat_velocity = 1.5 initial_hook_velocity = .2 app_port = 5000 ug_socket = {'ws': None} player_sockets = {} self.ug_socket = ug_socket self.player_sockets = player_sockets self.host = '' def make_player(ws): player = { 'type': 'boat', 'name': None, # predefine colours for up to six boats 'colour': [[randint(0, 255) for _ in range(3)], [90, 60, 20], [255, 255, 255]], 'hook_position': 0, 'hook_velocity': 0, 'xpos': randint(game_x_min, game_x_max), 'ypos': 8, 'velocity': 0, 'score': 0, 'id': ws.handler.client_address, 'catch': { 'colour': None, 'score': 0 } } self.player_sockets[ws.handler.client_address] = ws self.current_players[ws.handler.client_address] = player # inform the unity game about this? @sockets.route('/unity') def unity_client_socket(ws): # should check that unity is running locally only if ug_socket['ws'] is not None or ws.handler.client_address[ 0] != '127.0.0.1': return ug_socket['ws'] = ws ws.send("qr: " + self.host) while not ws.closed: # do stuff here i guess... message = ws.receive() if message is not None: if message == "": continue message = json.loads(str(message)) self.current_players[tuple( message['id'] )]['hook_velocity'] = -self.current_players[tuple( message['id'])]['hook_velocity'] self.current_players[tuple( message['id'])]['catch']['score'] = message['score'] self.current_players[tuple( message['id'])]['catch']['colour'] = [ int(s) for s in message['colour'][5:-4].split(',') ] self.player_sockets[tuple( message['id'])].send("fish_hooked") ug_socket['ws'] = None @sockets.route('/client') def device_client_socket(ws): make_player(ws) ws.send(json.dumps( self.current_players[ws.handler.client_address])) while not ws.closed: message = ws.receive() if message is None: break message = message.lower() player_state = self.current_players[ws.handler.client_address] # print("{}: {}".format(player_state['id'][0], message)) event_handlers = {'left': None} if message == 'left': if player_state['hook_velocity'] == 0: player_state['velocity'] = -boat_velocity elif message == 'right': if player_state['hook_velocity'] == 0: player_state['velocity'] = boat_velocity elif message == 'stop': player_state['velocity'] = 0 elif message == 'hook': # handle hook drop gameplay # player_state['score'] += 1 if player_state['hook_velocity'] == 0: player_state['velocity'] = 0 player_state['hook_velocity'] = initial_hook_velocity if self.ug_socket['ws'] is not None: self.ug_socket['ws'].send( 'hook dropped: ' + player_state['id'][0] + ' ' + str(player_state['id'][1])) elif message == 'plus': if player_state['hook_velocity'] > -.3: player_state['hook_velocity'] -= .04 elif message == 'minus': if player_state['hook_velocity'] < -.01: player_state['hook_velocity'] += .04 self.send_client_state(ws) del self.current_players[ws.handler.client_address] del self.player_sockets[ws.handler.client_address] import uuid qr_codes = set([uuid.uuid4().hex]) @app.route('/static/<path:filename>') def custom_static(filename): return send_from_directory(app.config['CUSTOM_STATIC_PATH'], filename) @app.route('/') def home(): # check they sent a a valid qr code # artworkpc.isd.ad.flinders.edu.au:5000/?qr=a6rt5grtg566bt qr = request.args.get('qr') # if qr not in qr_codes: # return "YOU CANT PLAY WITHOUT A QR CODE" with open('fishgame.html') as f: fish_html = f.read() return fish_html def flaskThread(): from gevent import pywsgi from geventwebsocket.handler import WebSocketHandler import socket host = socket.gethostbyname(socket.gethostname()) self.host = host server = pywsgi.WSGIServer(('0.0.0.0', app_port), app, handler_class=WebSocketHandler) # print("Starting server on: http://{}:{}".format(*server.address)) print("Starting server on: http://{}:{}".format( host, server.server_port)) # print host server.serve_forever() import thread thread.start_new_thread(flaskThread, ())
class FlaskWebSocket(object): def __init__(self, flask_app, on_message=None, on_connect=None, on_disconnect=None, url_prefix=r'/ws'): self._sockets = Sockets(flask_app) self._ws_bp = Blueprint(url_prefix, __name__) self._url_prefix = url_prefix self._clients = Clients() self.on_message = on_message self.on_connect = on_connect self.on_disconnect = on_disconnect self._register_routes() def _do_on_message(self, client, message): # Perform any custom message actions if self.on_message is not None: self.on_message(client, message) else: logger.info('Got message') def _do_on_connect(self, client): # Register client and perform any custom connection actions self._clients.add(client) if self.on_connect is not None: self.on_connect(client) def _do_on_disconnect(self, client): # Unregister client and perform any custom message actions self._clients.remove(client) if self.on_disconnect is not None: self.on_disconnect(client) def _register_routes(self): # This is here to test that the websocket is working properly while troubleshooting. @self._ws_bp.route('/echo') def echo(socket): logger.debug('Client Connected! {}'.format(request.remote_addr)) while not socket.closed: message = socket.receive() logger.debug('Got Message: {}'.format(message)) socket.send('got: {}'.format(message)) @self._ws_bp.route('/client') def client(socket): logger.debug('Client Connected! {}'.format(request.remote_addr)) self._do_on_connect(socket) while not socket.closed: message = socket.receive() logger.debug('Got Message: {}'.format(message)) if message is not None: self._do_on_message(socket, message) self._do_on_disconnect(socket) self._sockets.register_blueprint(self._ws_bp, url_prefix=self._url_prefix) def send_all(self, message): assert isinstance(message, str) self._clients.send_all(message) def __len__(self): return len(self._clients)
class RouteManager: def __init__(self): # Set up the app and the database self.app = Flask( __name__, template_folder=TEMPLATES_PATH, static_folder=STATIC_PATH, static_url_path="/static", ) self.sockets = Sockets(self.app) self.db = RethinkDB() self.log = logging.getLogger() self.app.secret_key = os.environ.get("WEBPAGE_SECRET_KEY", "super_secret") self.app.config["SERVER_NAME"] = os.environ.get( "SERVER_NAME", "pythondiscord.com:8080") self.app.before_request(self.db.before_request) self.app.teardown_request(self.db.teardown_request) # Load the main blueprint self.main_blueprint = Blueprint("main", __name__) self.log.debug(f"Loading Blueprint: {self.main_blueprint.name}") self.load_views(self.main_blueprint, "pysite/views/main") self.load_views(self.main_blueprint, "pysite/views/error_handlers") self.app.register_blueprint(self.main_blueprint) self.log.debug("") # Load the subdomains self.subdomains = ['api', 'staff'] for sub in self.subdomains: sub_blueprint = Blueprint(sub, __name__, subdomain=sub) self.log.debug(f"Loading Blueprint: {sub_blueprint.name}") self.load_views(sub_blueprint, f"pysite/views/{sub}") self.app.register_blueprint(sub_blueprint) # Load the websockets self.ws_blueprint = Blueprint("ws", __name__) self.log.debug("Loading websocket routes...") self.load_views(self.ws_blueprint, "pysite/views/ws") self.sockets.register_blueprint(self.ws_blueprint, url_prefix="/ws") def run(self): from gevent.pywsgi import WSGIServer from geventwebsocket.handler import WebSocketHandler server = WSGIServer(("", int(os.environ.get("WEBPAGE_PORT", 8080))), self.app, handler_class=WebSocketHandler) server.serve_forever() def load_views(self, blueprint, location="pysite/views"): for filename in os.listdir(location): if os.path.isdir(f"{location}/{filename}"): # Recurse if it's a directory; load ALL the views! self.load_views(blueprint, location=f"{location}/{filename}") continue if filename.endswith( ".py") and not filename.startswith("__init__"): module = importlib.import_module( f"{location}/{filename}".replace("/", ".")[:-3]) for cls_name, cls in inspect.getmembers(module): if (inspect.isclass(cls) and cls is not BaseView and cls is not ErrorView and cls is not RouteView and cls is not APIView and cls is not WS and (BaseView in cls.__mro__ or WS in cls.__mro__)): cls.setup(self, blueprint) self.log.debug( f">> View loaded: {cls.name: <15} ({module.__name__}.{cls_name})" )
from flask_sockets import Sockets import flask_login import json import inspect import requests import re import random _paragraph_re = re.compile(r'(?:\r\n|\r|\n){2,}') app = Flask(__name__) app.register_blueprint(matcher_blueprint) app.register_blueprint(admin_blueprint) app.register_blueprint(api_blueprint) sockets = Sockets(app) sockets.register_blueprint(ws) init_pager(app) login_manager = flask_login.LoginManager(app) login_manager.login_view = 'login_route' cat_to_ending = None osm_api_base = 'https://api.openstreetmap.org/api/0.6' really_save = True navbar_pages = { 'criteria_page': 'Criteria', 'tag_list': 'Search tags', 'documentation': 'Documentation', 'changesets': 'Recent changes', 'random_city': 'Random',
from view.article import article_bp from view.catalogue import catalogue_bp from view.graph import graph_bp from view.socket import socket_bp from view.tag import tag_bp from view.test import test_bp from view.user import user_bp from view.weekly import weekly_bp from view.workspace import workspace_bp app = Flask(__name__) log = Logger(__name__) app.config.from_object(config) CORS(app, supports_credentials=True) Session(app) sockets = Sockets(app) app.register_blueprint(test_bp, url_prefix='/test') app.register_blueprint(user_bp, url_prefix='/user') app.register_blueprint(article_bp, url_prefix='/article') app.register_blueprint(graph_bp, url_prefix='/graph') app.register_blueprint(tag_bp, url_prefix='/tag') app.register_blueprint(catalogue_bp, url_prefix='/catalogue') app.register_blueprint(album_bp, url_prefix='/album') app.register_blueprint(workspace_bp, url_prefix='/workspace') app.register_blueprint(weekly_bp, url_prefix='/weekly') sockets.register_blueprint(socket_bp, url_prefix='/') @app.route('/') def hello_world(): return 'Hello World!'
""" print("app logger is called '{}'".format(get_logger_name(app.logger))) print("EFF LEVEL {}".format(l.getEffectiveLevel())) print("LL is {}".format(l)) l.debug("debug level") l.info("info level") l.warn("warn level") l.error("error level") l.critical("critical level") logging.config.dictConfig(serverconfig.read_logging_config('logging.yaml')) the_main = None app = flask.Flask(__name__.split('.')[0]) test_logging(app.logger) socky = Sockets(app) # app.logger.debug('hoity toity') def gen_arglst() -> typing.List[str]: modtt = ('io', ) retlst = [] retlst.append('.iv -x') numvar = len(modtt) numcase = 2**numvar oodct = {True: 'on', False: 'off'} cmdstr1 = '.iv -al off -e on -r on -ie on -o 29' for icase in range(numcase): mask = 1 cmdstropt = '' for bit in range(numvar):
import os from celery import Celery from flask import Flask from flask_alembic import Alembic from flask_sqlalchemy import SQLAlchemy from flask_marshmallow import Marshmallow from flask_sockets import Sockets from server.tools import data_generator alembic = Alembic() db = SQLAlchemy() ma = Marshmallow() ws = Sockets() def create_app(config=None): config = config or os.environ['CONFIG'] app = Flask(__name__) app.config.from_object(config) alembic.init_app(app) db.init_app(app) ma.init_app(app) ws.init_app(app) from server import views from server import models app.register_blueprint(views.api.blueprint)
def create_socket(): socket = Sockets() chess_init_socket(socket) return socket
from backend.dl_model.ogg_to_wav import convert, convert_wav_to_ogg from backend.dl_model.recording import deep_learning_model from backend.messaging.cloud_db import ibm_db logging.basicConfig( format="%(asctime)s %(levelname)s %(message)s", level=logging.INFO) load_dotenv() db = None urls = Blueprint("urls", "urls", "static") ws = Blueprint('ws', __name__) sockets = Sockets() sockets.all_conns = [] logging.info('connected to RMQ') # On IBM Cloud Cloud Foundry, get the port number from the environment variable PORT # When running this app on the local machine, default the port to 8000 port = int(os.getenv('PORT', 5000)) @urls.route('/') def root(): return urls.send_static_file('index.html') # /* Endpoint to greet and add a new visitor to database.
from flask_sockets import Sockets from ErinaServer.Server import ErinaServer ErinaSockets = Sockets(ErinaServer)
from flask import Flask, render_template, request from flask_sockets import Sockets from gevent import pywsgi from geventwebsocket.handler import WebSocketHandler from sonoff.websocketsrv import WebSocketSrv from sonoff.websockclient import Websocketclient from config.config import Config import json import requests import threading import sonoff.wsclientglb flaskapp = Flask(__name__) socket = Sockets(flaskapp) @flaskapp.route('/') def home(): return render_template('main.html') @flaskapp.route('/dispatch/device', methods=['GET']) def sonoffDispatchDeviceGet(): print( "REST: Relay attempts to get websocket serever address from GET /dispatch/device" ) jsonresult = {"error": 0, "reason": "ok", "IP": "192.168.1.2", "port": 443} return json.dumps(jsonresult)
class Han: def __init__(self, flask_app, initial_state, debug=False): self.current_state_key = key = uuid() self.state_entries = [initial_state_entry(key, initial_state)] self.handlers = {} self.state_updates = [] self.sockets = Sockets(flask_app) self.add_api() if debug: self.add_debug_api(flask_app) def add_api(self): "Add the websocket routes to let Han communicate with the frontend." self.sockets.route("/state/<path>")(self.state_updates_route) self.sockets.route("/action")(self.actions_route) def add_debug_api(self, app): app.route("/debug")(self.debug_route) app.route("/debug/states")(self.return_states_route) app.route("/debug/state", methods=["POST"])(self.set_state_route) @property def state_entry(self): "Getter for the 'state_entry' property." for state_entry in self.state_entries: if state_entry.key == self.current_state_key: return state_entry raise KeyError("Current state doesn't exist!") @property def state(self): return self.state_entry.state @staticmethod def debug_route(): "Send the Debug HTML page." return flask.send_file("han/debug/debug.html") def set_state_route(self): "Debug route for setting the current state." data = flask.request.get_json(force=True) self.current_state_key = data["id"] entry = self.state_entry self.update_all_clients(entry.path, entry.new_data) return flask.jsonify({"message": "Done!"}) def update_all_clients(self, path, new_state): "Send a state update to all clients." for update_queue in self.state_updates: update_queue.put(StateUpdate(path, new_state)) def return_states_route(self): "Return a list of states, along with the actions which caused them." return flask.jsonify({ "states": [{ "state": entry.state, "action": entry.action, "id": entry.key, "diff": { "path": entry.path, "data": entry.new_data, "old_data": entry.old_data } } for entry in self.state_entries] }) def dispatch_action(self, action): "Dispatch an action to a handler, if there is one. Update our state." properties = {k: v for (k, v) in action.items() if k != "type"} handler, input_path, output_path = self.handlers[action["type"]] input_data = value_at(self.state, input_path) output_data = handler(input_data, **properties) self.update_all_clients(output_path, output_data) self.set_state_at(output_path, output_data, action) def set_state_at(self, path, new_data, action): "Write `data` to the state at the given JSON path." # What happens if the current state isn't the last entry? if self.state_entries[-1].key != self.current_state_key: raise NotImplementedError( "Illegal state: write some branch logic!") # Store the data. old_data = value_at(self.state, path) # Set, or completely replace, the current state. if path == "$": new_state = new_data else: new_state = set_value_at(self.state, path, new_data) # Create a new state entry with the data changes. new_id = uuid() self.state_entries.append( StateEntry(new_id, action, new_state, path, new_data, old_data)) self.current_state_key = new_id def state_updates_route(self, socket, path): "Send state updates over a websocket connection." updates = Queue() self.state_updates.append(updates) # Send an initial state. socket.send(json.dumps(value_at(self.state, path))) while not socket.closed: state_update = updates.get() if mutual_contains(path, state_update.path): socket.send(json.dumps(value_at(self.state, path))) def actions_route(self, socket): "Receive actions over a websocket connection." while not socket.closed: action = socket.receive() if action: self.dispatch_action(json.loads(action)) def dle(self, action, input_path="$", output_path=None): "Return something which maps a given function to an action." def map_action_to(function): """ When `action` is dispatched, we should call `function` with arguments `state[input_path]` and any properties in the action. Then take the result, and assign it to `state[output_path]`. """ out_path = output_path or input_path self.handlers[action.name] = ActionHandler(function, input_path, out_path) return function return map_action_to
from api.websocket_blueprint import construct_websocket_blueprint from config import FIREBASE_CONFIG from api.device_blueprint import construct_device_blueprint from api.spell_blueprint import construct_spell_blueprint from api.area_blueprint import construct_area_blueprint from api.lumos_exception import LumosException app = Flask(__name__) app.url_map.strict_slashes = False CORS(app) monkey.patch_all() app.debug = True sockets = Sockets(app) firebase = pyrebase.initialize_app(FIREBASE_CONFIG) database = firebase.database() app.register_blueprint(construct_device_blueprint(database)) app.register_blueprint(construct_spell_blueprint(database)) app.register_blueprint(construct_area_blueprint(database)) sockets.register_blueprint(construct_websocket_blueprint(database)) # lumos-web @app.route('/', methods=['GET']) def serve_lumos_web_index(): return app.send_static_file('index.html')
def create_app(handle, instance, reloader=None): check.inst_param(handle, 'handle', ExecutionTargetHandle) check.inst_param(instance, 'instance', DagsterInstance) check.opt_inst_param(reloader, 'reloader', Reloader) app = Flask('dagster-ui') sockets = Sockets(app) app.app_protocol = lambda environ_path_info: 'graphql-ws' schema = create_schema() subscription_server = DagsterSubscriptionServer(schema=schema) execution_manager = SubprocessExecutionManager(instance) print('Loading repository...') context = DagsterGraphQLContext( handle=handle, instance=instance, execution_manager=execution_manager, reloader=reloader, version=__version__, ) # Automatically initialize scheduler everytime Dagit loads scheduler_handle = context.scheduler_handle if scheduler_handle: handle = context.get_handle() python_path = sys.executable repository_path = handle.data.repository_yaml scheduler_handle.up(python_path, repository_path) app.add_url_rule( '/graphql', 'graphql', DagsterGraphQLView.as_view( 'graphql', schema=schema, graphiql=True, # XXX(freiksenet): Pass proper ws url graphiql_template=PLAYGROUND_TEMPLATE, executor=Executor(), context=context, ), ) sockets.add_url_rule( '/graphql', 'graphql', dagster_graphql_subscription_view(subscription_server, context)) app.add_url_rule( # should match the `build_local_download_url` '/download/<string:run_id>/<string:step_key>/<string:file_type>', 'download_view', download_view(context), ) # these routes are specifically for the Dagit UI and are not part of the graphql # API that we want other people to consume, so they're separate for now. # Also grabbing the magic global request args dict so that notebook_view is testable app.add_url_rule('/dagit/notebook', 'notebook', lambda: notebook_view(request.args)) app.add_url_rule('/static/<path:path>/<string:file>', 'static_view', static_view) app.add_url_rule('/vendor/<path:path>/<string:file>', 'vendor_view', vendor_view) app.add_url_rule('/<path:_path>', 'index_catchall', index_view) app.add_url_rule('/', 'index', index_view, defaults={'_path': ''}) CORS(app) return app
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU Affero General Public License for more details. # # You should have received a copy of the GNU Affero General Public License # along with PrdTest. If not, see <https://www.gnu.org/licenses/>. from flask import Flask from flask_breadcrumbs import Breadcrumbs, default_breadcrumb_root from flask_sockets import Sockets from gevent import pywsgi from geventwebsocket.handler import WebSocketHandler from prdtest.views import home, binary_pk, rng_assisted_divination app = Flask(__name__) sockets = Sockets(app) Breadcrumbs(app) default_breadcrumb_root(home.blueprint, '.') app.register_blueprint(home.blueprint) app.register_blueprint(binary_pk.blueprint, url_prefix='/binary_pk') sockets.register_blueprint(binary_pk.ws_blueprint, url_prefix='/binary_pk') app.register_blueprint(rng_assisted_divination.blueprint, url_prefix='/rng_assisted_divination') sockets.register_blueprint(rng_assisted_divination.ws_blueprint, url_prefix='/rng_assisted_divination') server = pywsgi.WSGIServer(('0.0.0.0', 57011),
def __init__(self): # Set up the app and the database self.app = Flask( __name__, template_folder=TEMPLATES_PATH, static_folder=STATIC_PATH, static_url_path="/static", ) self.sockets = Sockets(self.app) self.db = RethinkDB() self.log = logging.getLogger() self.app.secret_key = os.environ.get("WEBPAGE_SECRET_KEY", "super_secret") self.app.config["SERVER_NAME"] = os.environ.get( "SERVER_NAME", "pythondiscord.local:8080") self.app.config["PREFERRED_URL_SCHEME"] = PREFERRED_URL_SCHEME self.app.before_request(self.db.before_request) self.app.teardown_request(self.db.teardown_request) # Load the oauth blueprint self.oauth_backend = OauthBackend(self) self.oauth_blueprint = make_discord_blueprint( DISCORD_OAUTH_ID, DISCORD_OAUTH_SECRET, DISCORD_OAUTH_SCOPE, '/', login_url=DISCORD_OAUTH_REDIRECT, authorized_url=DISCORD_OAUTH_AUTHORIZED, backend=self.oauth_backend) self.log.debug(f"Loading Blueprint: {self.oauth_blueprint.name}") self.app.register_blueprint(self.oauth_blueprint) self.log.debug("") # Load the main blueprint self.main_blueprint = Blueprint("main", __name__) self.log.debug(f"Loading Blueprint: {self.main_blueprint.name}") self.load_views(self.main_blueprint, "pysite/views/main") self.load_views(self.main_blueprint, "pysite/views/error_handlers") self.app.register_blueprint(self.main_blueprint) self.log.debug("") # Load the subdomains self.subdomains = ['api', 'staff'] for sub in self.subdomains: sub_blueprint = Blueprint(sub, __name__, subdomain=sub) self.log.debug(f"Loading Blueprint: {sub_blueprint.name}") self.load_views(sub_blueprint, f"pysite/views/{sub}") try: self.app.register_blueprint(sub_blueprint) except Exception: logging.getLogger(__name__).exception( f"Failed to register blueprint for subdomain: {sub}") # Load the websockets self.ws_blueprint = Blueprint("ws", __name__) self.log.debug("Loading websocket routes...") self.load_views(self.ws_blueprint, "pysite/views/ws") self.sockets.register_blueprint(self.ws_blueprint, url_prefix="/ws") self.app.before_request( self.https_fixing_hook) # Try to fix HTTPS issues
app = Flask(__name__) app.secret_key = 'feca0226-1746-6666-92ac-1999e1eea085' @app.route('/') def index(): return 'The flexx app is under the flexx folder: <a href="/flexx/">your.server.com/flexx</a>' @app.route('/stop') def stop(): global server server.stop() return 'stopping' ########### Register apps ########### app.register_blueprint(FlexxBlueprint, url_prefix='/flexx') ########### Register sockets ########### sockets = Sockets(app) # keep at the end sockets.register_blueprint(FlexxWS, url_prefix='/flexx') # from gevent import monkey; monkey.patch_all() # moved at beginning from gevent import pywsgi from geventwebsocket.handler import WebSocketHandler import platform server = pywsgi.WSGIServer(('127.0.0.1', 5000), app, handler_class=WebSocketHandler) print("Server Started!") server.serve_forever()
#office #app.config['SQLALCHEMY_DATABASE_URI']= 'postgresql://*****:*****@localhost:5432/loopme' #home #app.config['SQLALCHEMY_DATABASE_URI']= 'postgresql://*****:*****@localhost:5432/postgres' #production app.config['SQLALCHEMY_DATABASE_URI']= 'postgresql://*****:*****@ec2-174-129-29-118.compute-1.amazonaws.com:5432/dfini92nqen64n' app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False app.secret_key = 'some_secret' db = SQLAlchemy(app) print "db created" """Create Database migrations""" migrate = Migrate(app, db) manager = Manager(app) manager.add_command('db', MigrateCommand) print "db migrated" from router import router, ws app.register_blueprint(router) sockets = Sockets(app) sockets.register_blueprint(ws) #@app.route('/') #def root(): # return app.send_static_file('index.html')
else: print("Operating system not supported, please try on Mac OS or Windows") sys.exit(1) else: app = Flask(__name__, static_folder="static", template_folder="templates") CORS(app) # Disables caching for each flair app that uses PyWebView app.config["SEND_FILE_MAX_AGE_DEFAULT"] = 1 # Registers webview API's under /webview/<api-name> to keep code separate and clean app.register_blueprint(webview_bp, url_prefix='/webview_bp') app.register_blueprint(example_bp, url_prefix='/example_bp') ws = Sockets(app) ws.register_blueprint(example_ws, url_prefix='/example_ws') @app.after_request def add_header(response): """ Disables caching for each flair app that uses PyWebView """ response.headers['Cache-Control'] = 'no-store' return response @app.route("/") def home():
# -*- coding: utf-8 -*- from flask import Flask from flask_sqlalchemy import SQLAlchemy from flask_login import LoginManager from flask_sockets import Sockets from app.config import config_dict, base from celery import Celery db = SQLAlchemy() login_manager = LoginManager() login_manager.session_protection = 'strong' login_manager.login_view = 'main_auth.admin_login' login_manager.login_message = u'请先登录' sockets = Sockets() celery = Celery(__name__, broker=base.CELERY_BROKER_URL) #: config_name:配置名称 def create_app(config_name): app = Flask(__name__) app.config.from_object(config_dict[config_name]) db.init_app(app) register_blueprints(app) sockets.init_app(app) from gevent import pywsgi from geventwebsocket.handler import WebSocketHandler
from flask import Flask from flask_sockets import Sockets from gevent import pywsgi from geventwebsocket.handler import WebSocketHandler import telebot app = Flask(__name__, static_url_path='', static_folder='static', template_folder='templates') sockets = Sockets(app) bot = telebot.TeleBot("1769481990:AAG64HR9lYBU11JYflC3C4plU2Yb-Ao5so4", parse_mode="MARKDOWN") @sockets.route('/echo') def echo_socket(ws): while True: message = ws.receive() ws.send(message[::-1]) from app import routes from app import flow if __name__ == '__main__': sockets.run()
def __init__(self): self._resources_dir: Path = Path(__file__).parent / 'resources' self._app: Flask = Flask( __name__, static_folder=self._resources_dir / 'static') self._sockets: Sockets = Sockets(self._app)
def create_app( node_id: str, debug=False, n_replica=None, test_config=None, data_dir=None, config_file=None, mean_std_file: str = None, ) -> Flask: """Create flask application. Args: node_id: ID used to identify this node. debug: debug mode flag. n_replica: Number of model replicas used for fault tolerance purposes. test_config: database test settings. Returns: app : Flask App instance. """ app = Flask(__name__) app.debug = debug app.config["SECRET_KEY"] = os.environ.get("SECRET_KEY", None) if app.config["SECRET_KEY"] is None: app.config["SECRET_KEY"] = DEFAULT_SECRET_KEY logging.warning( "Using default secrect key, this is not safe and should be used only for testing and development. To define a secrete key please define the environment variable SECRET_KEY." ) app.config["N_REPLICA"] = n_replica sockets = Sockets(app) # Register app blueprints from .main import ( auth, data_centric_routes, hook, local_worker, main_routes, model_centric_routes, ws, ) # set_node_id(id) local_worker.id = node_id hook.local_worker._known_workers[node_id] = local_worker local_worker.add_worker(hook.local_worker) # add data if data_dir: import configparser import albumentations as a from torchlib.utils import Arguments, AddGaussianNoise, To_one_hot, MixUp from torchlib.dataloader import ( AlbumentationsTorchTransform, CombinedLoader, LabelMNIST, calc_mean_std, create_albu_transform, PathDataset, ) from os import path from argparse import Namespace from random import seed as r_seed loader = CombinedLoader() config = configparser.ConfigParser() assert path.isfile(config_file), "config file not found" config.read(config_file) cmd_args = Namespace( data_dir=data_dir, visdom=False, cuda=False, train_federated=True, websockets=True, verbose=True, unencrypted_aggregation=False, ) args = Arguments(cmd_args, config, mode="train", verbose=False) # torch.manual_seed(args.seed) # r_seed(args.seed) # np.random.seed(args.seed) # torch.backends.cudnn.deterministic = True # torch.backends.cudnn.benchmark = False if node_id == "data_owner": print("setting up an data to be remotely classified.") tf = [ a.Resize(args.inference_resolution, args.inference_resolution), a.CenterCrop(args.inference_resolution, args.inference_resolution), ] if hasattr(args, "clahe") and args.clahe: tf.append(a.CLAHE(always_apply=True, clip_limit=(1, 1))) if mean_std_file: mean_std = torch.load(mean_std_file) if type(mean_std) == dict and "val_mean_std" in mean_std: mean_std = mean_std["val_mean_std"] mean, std = mean_std else: raise RuntimeError( "To set up a data owner for inference we need a file which tells" " us how to normalize the data." ) tf.extend( [ a.ToFloat(max_value=255.0), a.Normalize( mean.cpu().numpy()[None, None, :], std.cpu().numpy()[None, None, :], max_pixel_value=1.0, ), ] ) tf = AlbumentationsTorchTransform(a.Compose(tf)) loader = CombinedLoader() dataset = PathDataset(data_dir, transform=tf, loader=loader,) data = [] for d in tqdm(dataset, total=len(dataset), leave=False, desc="load data"): data.append(d) data = torch.stack(data) # pylint:disable=no-member data.tag("#inference_data") local_worker.load_data([data]) print("Loaded {:d} samples as inference data".format(data.shape[0])) else: if args.data_dir == "mnist": node_id = local_worker.id KEEP_LABELS_DICT = { "alice": [0, 1, 2, 3], "bob": [4, 5, 6], "charlie": [7, 8, 9], None: list(range(10)), } dataset = LabelMNIST( labels=KEEP_LABELS_DICT[node_id] if node_id in KEEP_LABELS_DICT else KEEP_LABELS_DICT[None], root="./data", train=True, download=True, transform=transforms.Compose( [ transforms.ToTensor(), transforms.Normalize((0.1307,), (0.3081,)), ] ), ) else: stats_dataset = ImageFolder( data_dir, loader=loader, transform=transforms.Compose( [ transforms.Resize(args.train_resolution), transforms.CenterCrop(args.train_resolution), transforms.ToTensor(), ] ), ) assert ( len(stats_dataset.classes) == 3 ), "We can only handle data that has 3 classes: normal, bacterial and viral" mean, std = calc_mean_std(stats_dataset, save_folder=data_dir,) del stats_dataset target_tf = None if args.mixup or args.weight_classes: target_tf = [ lambda x: torch.tensor(x), # pylint:disable=not-callable To_one_hot(3), ] dataset = ImageFolder( # path.join("data/server_simulation/", "validation") # if worker.id == "validation" # else data_dir, loader=loader, transform=create_albu_transform(args, mean, std), target_transform=transforms.Compose(target_tf) if target_tf else None, ) assert ( len(dataset.classes) == 3 ), "We can only handle data that has 3 classes: normal, bacterial and viral" mean.tag("#datamean") std.tag("#datastd") local_worker.load_data([mean, std]) data, targets = [], [] # repetitions = 1 if worker.id == "validation" else args.repetitions_dataset if args.mixup: dataset = torch.utils.data.DataLoader( dataset, batch_size=1, shuffle=True ) mixup = MixUp(λ=args.mixup_lambda, p=args.mixup_prob) last_set = None for j in tqdm( range(args.repetitions_dataset), total=args.repetitions_dataset, leave=False, desc="register data on {:s}".format(local_worker.id), ): for d, t in tqdm( dataset, total=len(dataset), leave=False, desc="register data {:d}. time".format(j + 1), ): if args.mixup: original_set = (d, t) if last_set: # pylint:disable=unsubscriptable-object d, t = mixup(((d, last_set[0]), (t, last_set[1]))) last_set = original_set data.append(d) targets.append(t) selected_data = torch.stack(data) # pylint:disable=no-member selected_targets = ( torch.stack(targets) # pylint:disable=no-member if args.mixup or args.weight_classes else torch.tensor(targets) # pylint:disable=not-callable ) if args.mixup: selected_data = selected_data.squeeze(1) selected_targets = selected_targets.squeeze(1) del data, targets selected_data.tag( "#traindata", ) # "#valdata" if worker.id == "validation" else selected_targets.tag( # "#valtargets" if worker.id == "validation" else "#traintargets", ) local_worker.load_data([selected_data, selected_targets]) print( "registered {:d} samples of {:s} data".format( selected_data.size(0), args.data_dir ) ) del selected_data, selected_targets # Register app blueprints app.register_blueprint(main_routes, url_prefix=r"/") app.register_blueprint(model_centric_routes, url_prefix=r"/model-centric") app.register_blueprint(data_centric_routes, url_prefix=r"/data-centric") sockets.register_blueprint(ws, url_prefix=r"/") # Set SQLAlchemy configs set_database_config(app, test_config=test_config) s = app.app_context().push() if database_exists(db.engine.url): db.create_all() else: db.create_all() seed_db() db.session.commit() # Set Authentication configs app = auth.set_auth_configs(app) CORS(app) # Threads executor.init_app(app) app.config["EXECUTOR_PROPAGATE_EXCEPTIONS"] = True app.config["EXECUTOR_TYPE"] = "thread" return app
def create_app(node_id, debug=False, database_url=None, data_dir: str = None): """ Create / Configure flask socket application instance. Args: node_id (str) : ID of Grid Node. debug (bool) : debug flag. test_config (bool) : Mock database environment. Returns: app : Flask application instance. """ app = Flask(__name__) app.debug = debug app.config["SECRET_KEY"] = "justasecretkeythatishouldputhere" # Enable persistent mode # Overwrite syft.object_storage methods to work in a persistent way # Persist models / tensors if database_url: app.config["REDISCLOUD_URL"] = database_url from .main.persistence import database, object_storage db_instance = database.set_db_instance(database_url) object_storage.set_persistent_mode(db_instance) from .main import html, ws, hook, local_worker, auth # Global socket handler sockets = Sockets(app) # set_node_id(id) local_worker.id = node_id hook.local_worker._known_workers[node_id] = local_worker local_worker.add_worker(hook.local_worker) # add data if data_dir: print("register data") if "mnist" in data_dir.lower(): dataset = MNIST( root="./data", train=True, download=True, transform=transforms.Compose( [transforms.ToTensor(), transforms.Normalize((0.1307,), (0.3081,))] ), ) if node_id in KEEP_LABELS_DICT: indices = np.isin(dataset.targets, KEEP_LABELS_DICT[node_id]).astype( "uint8" ) selected_data = ( torch.native_masked_select( # pylint:disable=no-member dataset.data.transpose(0, 2), torch.tensor(indices), # pylint:disable=not-callable ) .view(28, 28, -1) .transpose(2, 0) ) selected_targets = torch.native_masked_select( # pylint:disable=no-member dataset.targets, torch.tensor(indices), # pylint:disable=not-callable ) """ dataset = sy.BaseDataset( data=selected_data, targets=selected_targets, transform=dataset.transform, ) dataset_name = "mnist" """ else: train_tf = [ transforms.RandomVerticalFlip(p=0.5), transforms.RandomAffine( degrees=30, translate=(0, 0), scale=(0.85, 1.15), shear=10, # fillcolor=0.0, ), transforms.Resize(224), transforms.RandomCrop(224), transforms.ToTensor(), transforms.Normalize((0.57282609,), (0.17427578,)), # transforms.RandomApply([AddGaussianNoise(mean=0.0, std=0.05)], p=0.5), ] """train_tf.append( transforms.Lambda( lambda x: torch.repeat_interleave( # pylint: disable=no-member x, 3, dim=0 ) ) )""" target_dict_pneumonia = {0: 1, 1: 0, 2: 2} dataset = ImageFolder( data_dir, transform=transforms.Compose(train_tf), target_transform=lambda x: target_dict_pneumonia[x], ) data, targets = [], [] for d, t in tqdm(dataset, total=len(dataset)): data.append(d) targets.append(t) selected_data = torch.stack(data) # pylint:disable=no-member selected_targets = torch.from_numpy(np.array(targets)) # pylint:disable=no-member #dataset = sy.BaseDataset(data=data, targets=targets) """dataset = PPPP( "data/Labels.csv", train=True, transform=transforms.Compose(train_tf), seed=1 )""" dataset_name = "pneumonia" local_worker.register_obj(selected_data, "data") local_worker.register_obj(selected_targets, "targets") print("registered data") # Register app blueprints app.register_blueprint(html, url_prefix=r"/") sockets.register_blueprint(ws, url_prefix=r"/") # Set Authentication configs app = auth.set_auth_configs(app) return app
def create_app(node_id: str, debug=False, n_replica=None, test_config=None) -> Flask: """Create flask application. Args: node_id: ID used to identify this node. debug: debug mode flag. n_replica: Number of model replicas used for fault tolerance purposes. test_config: database test settings. Returns: app : Flask App instance. """ app = Flask(__name__) app.debug = debug app.config["SECRET_KEY"] = os.environ.get("SECRET_KEY", None) if app.config["SECRET_KEY"] is None: app.config["SECRET_KEY"] = DEFAULT_SECRET_KEY logging.warning( "Using default secrect key, this is not safe and should be used only for testing and development. To define a secrete key please define the environment variable SECRET_KEY." ) app.config["N_REPLICA"] = n_replica sockets = Sockets(app) # Register app blueprints from .main import ( auth, data_centric_routes, hook, local_worker, main_routes, model_centric_routes, ws, ) # set_node_id(id) local_worker.id = node_id hook.local_worker._known_workers[node_id] = local_worker local_worker.add_worker(hook.local_worker) # Register app blueprints app.register_blueprint(main_routes, url_prefix=r"/") app.register_blueprint(model_centric_routes, url_prefix=r"/model-centric") app.register_blueprint(data_centric_routes, url_prefix=r"/data-centric") sockets.register_blueprint(ws, url_prefix=r"/") # Set SQLAlchemy configs set_database_config(app, test_config=test_config) s = app.app_context().push() if database_exists(db.engine.url): db.create_all() else: db.create_all() seed_db() db.session.commit() # Set Authentication configs app = auth.set_auth_configs(app) CORS(app) # Threads executor.init_app(app) app.config["EXECUTOR_PROPAGATE_EXCEPTIONS"] = True app.config["EXECUTOR_TYPE"] = "thread" return app