def _start_tornado(ibs_, port_): # Get Flask app app = controller_inject.get_flask_app() app.ibs = ibs_ # Try to ascertain the socket's domain name try: app.server_domain = socket.gethostbyname(socket.gethostname()) except socket.gaierror: app.server_domain = '127.0.0.1' app.server_port = port_ # URL for the web instance app.server_url = 'http://%s:%s' % (app.server_domain, app.server_port) print('[web] Tornado server starting at %s' % (app.server_url,)) # Launch the web browser to view the web interface and API if browser: url = app.server_url + url_suffix import webbrowser print('[web] opening browser with url = %r' % (url,)) webbrowser.open(url) # Start the tornado web handler # WSGI = Web Server Gateway Interface # WSGI is Python standard described in detail in PEP 3333 http_server = tornado.httpserver.HTTPServer( tornado.wsgi.WSGIContainer(app)) http_server.listen(app.server_port) tornado.ioloop.IOLoop.instance().start()
def _start_tornado(ibs_, port_): # Get Flask app app = controller_inject.get_flask_app() app.ibs = ibs_ # Try to ascertain the socket's domain name try: app.server_domain = socket.gethostbyname(socket.gethostname()) except socket.gaierror: app.server_domain = '127.0.0.1' app.server_port = port_ # URL for the web instance app.server_url = 'http://%s:%s' % (app.server_domain, app.server_port) print('[web] Tornado server starting at %s' % (app.server_url, )) # Launch the web browser to view the web interface and API if browser: url = app.server_url + url_suffix import webbrowser print('[web] opening browser with url = %r' % (url, )) webbrowser.open(url) # Start the tornado web handler # WSGI = Web Server Gateway Interface # WSGI is Python standard described in detail in PEP 3333 http_server = tornado.httpserver.HTTPServer( tornado.wsgi.WSGIContainer(app)) http_server.listen(app.server_port) tornado.ioloop.IOLoop.instance().start()
def template(template_directory=None, template_filename=None, **kwargs): ibs = current_app.ibs global_args = { 'NAVBAR': NavbarClass(), 'YEAR': date.today().year, 'URL': flask.request.url, 'REFER_SRC_STR': flask.request.url.replace(flask.request.url_root, ''), '__login__': flask.request.url_rule.rule == url_for('login'), '__wrapper__': True, '__wrapper_header__': True, '__wrapper_footer__': True, '__containerized__': ibs.containerized, '__https__': ibs.https, 'user': controller_inject.get_user(), } global_args['REFER_SRC_ENCODED'] = encode_refer_url( global_args['REFER_SRC_STR']) if 'refer' in flask.request.args.keys(): refer = flask.request.args['refer'] print('[web] REFER: %r' % (refer, )) global_args['REFER_DST_ENCODED'] = refer global_args['REFER_DST_STR'] = decode_refer_url(refer) if template_directory is None: template_directory = '' #template_directory = abspath(join(dirname(__file__), 'templates')) #template_directory = join(dirname(dirname(__file__))) if template_filename is None: template_filename = 'index' template_ = join(template_directory, template_filename + '.html') # Update global args with the template's args _global_args = dict(global_args) _global_args.update(kwargs) print('[appfuncs] template()') app = controller_inject.get_flask_app() # flask hates windows apparently template_ = template_.replace('\\', '/') print('[appfuncs.template] * app.template_folder = %r' % (app.template_folder, )) print('[appfuncs.template] * template_directory = %r' % (template_directory, )) print('[appfuncs.template] * template_filename = %r' % (template_filename, )) print('[appfuncs.template] * template_ = %r' % (template_, )) try: ret = flask.render_template(template_, **_global_args) #ret = flask.render_template(full_template_fpath, **_global_args) except jinja2.exceptions.TemplateNotFound as ex: print('Error template not found') full_template_fpath = join(app.template_folder, template_) print('[appfuncs.template] * full_template_fpath = %r' % (full_template_fpath, )) ut.checkpath(full_template_fpath, verbose=True) ut.printex(ex, 'Template error in appfuncs', tb=True) raise except Exception as ex: ut.printex(ex, 'Error in appfuncs', tb=True) raise return ret
def _start_tornado(ibs_, port_): # Get Flask app app = controller_inject.get_flask_app() app.ibs = ibs_ # Try to ascertain the socket's domain name socket.setdefaulttimeout(0.1) try: app.server_domain = socket.gethostbyname(socket.gethostname()) except socket.gaierror: app.server_domain = '127.0.0.1' socket.setdefaulttimeout(None) app.server_port = port_ # URL for the web instance app.server_url = 'http://%s:%s' % (app.server_domain, app.server_port) print('[web] Tornado server starting at %s' % (app.server_url, )) # Launch the web browser to view the web interface and API if browser: url = app.server_url + url_suffix import webbrowser print('[web] opening browser with url = %r' % (url, )) webbrowser.open(url) # Start the tornado web handler # WSGI = Web Server Gateway Interface # WSGI is Python standard described in detail in PEP 3333 wsgi_container = TimedWSGIContainer(app) # # Try wrapping with newrelic performance monitoring # try: # import newrelic # wsgi_container = newrelic.agent.WSGIApplicationWrapper(wsgi_container) # except (ImportError, AttributeError): # pass http_server = tornado.httpserver.HTTPServer(wsgi_container) try: http_server.listen(app.server_port) except socket.error: fallback_port = ut.find_open_port(app.server_port) if fallback: print('Port %s is unavailable, using fallback_port = %r' % ( port, fallback_port, )) start_tornado(ibs, port=fallback_port, browser=browser, url_suffix=url_suffix, start_web_loop=start_web_loop, fallback=False) else: raise RuntimeError( (('The specified IBEIS web port %d is not available, ' 'but %d is') % (app.server_port, fallback_port))) if start_web_loop: tornado.ioloop.IOLoop.instance().start()
def template(template_directory=None, template_filename=None, **kwargs): global_args = { 'NAVBAR': NavbarClass(), 'YEAR': date.today().year, 'URL': flask.request.url, 'REFER_SRC_STR': flask.request.url.replace(flask.request.url_root, ''), '__wrapper__' : True, } global_args['REFER_SRC_ENCODED'] = encode_refer_url(global_args['REFER_SRC_STR']) if 'refer' in flask.request.args.keys(): refer = flask.request.args['refer'] print('[web] REFER: %r' % (refer, )) global_args['REFER_DST_ENCODED'] = refer global_args['REFER_DST_STR'] = decode_refer_url(refer) if template_directory is None: template_directory = '' #template_directory = abspath(join(dirname(__file__), 'templates')) #template_directory = join(dirname(dirname(__file__))) if template_filename is None: template_filename = 'index' template_ = join(template_directory, template_filename + '.html') # Update global args with the template's args _global_args = dict(global_args) _global_args.update(kwargs) print('[appfuncs] template()') from ibeis.control import controller_inject app = controller_inject.get_flask_app() # flask hates windows apparently template_ = template_.replace('\\', '/') print('[appfuncs.template] * app.template_folder = %r' % (app.template_folder,)) print('[appfuncs.template] * template_directory = %r' % (template_directory,)) print('[appfuncs.template] * template_filename = %r' % (template_filename,)) print('[appfuncs.template] * template_ = %r' % (template_,)) try: ret = flask.render_template(template_, **_global_args) #ret = flask.render_template(full_template_fpath, **_global_args) except jinja2.exceptions.TemplateNotFound as ex: print('Error template not found') full_template_fpath = join(app.template_folder, template_) print('[appfuncs.template] * full_template_fpath = %r' % (full_template_fpath,)) ut.checkpath(full_template_fpath, verbose=True) ut.printex(ex, 'Template error in appfuncs', tb=True) raise except Exception as ex: ut.printex(ex, 'Error in appfuncs', tb=True) raise return ret
def opendb_fg_web(*args, **kwargs): """ Ignore: >>> from ibeis.main_module import * # NOQA >>> kwargs = {'db': 'testdb1'} >>> args = tuple() >>> import ibeis >>> ibs = ibeis.opendb_fg_web() """ # Gives you context inside the web app for testing kwargs['start_web_loop'] = False kwargs['web'] = True kwargs['browser'] = False ibs = opendb(*args, **kwargs) from ibeis.control import controller_inject app = controller_inject.get_flask_app() ibs.app = app return ibs