#NEWLINE# def make_app( ) : #NEWLINE# #INDENT# config = Config ( ) #NEWLINE# config . NbconvertApp . fileext = 'html' #NEWLINE# config . CSSHTMLHeaderTransformer . enabled = False #NEWLINE# if os . environ . get ( 'DEBUG' ) : #NEWLINE# #INDENT# options . debug = True #NEWLINE# logging . getLogger ( ) . setLevel ( logging . DEBUG ) #NEWLINE# #UNINDENT# mc_pool = ThreadPoolExecutor ( options . mc_threads ) #NEWLINE# formats = configure_formats ( options , config , log . app_log ) #NEWLINE# if options . processes : #NEWLINE# #INDENT# pool = ProcessPoolExecutor ( options . processes ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# pool = ThreadPoolExecutor ( options . threads ) #NEWLINE# #UNINDENT# memcache_urls = os . environ . get ( 'MEMCACHIER_SERVERS' , os . environ . get ( 'MEMCACHE_SERVERS' ) ) #NEWLINE# if os . environ . get ( 'NBCACHE_PORT' ) : #NEWLINE# #INDENT# tcp_memcache = os . environ . get ( 'NBCACHE_PORT' ) #NEWLINE# memcache_urls = tcp_memcache . split ( 'tcp://' ) [ 1 ] #NEWLINE# #UNINDENT# if os . environ . get ( 'NBINDEX_PORT' ) : #NEWLINE# #INDENT# log . app_log . info ( 'Indexing notebooks' ) #NEWLINE# tcp_index = os . environ . get ( 'NBINDEX_PORT' ) #NEWLINE# index_url = tcp_index . split ( 'tcp://' ) [ 1 ] #NEWLINE# ( index_host , index_port ) = index_url . split ( ':' ) #NEWLINE# indexer = ElasticSearch ( index_host , index_port ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# log . app_log . info ( 'Not indexing notebooks' ) #NEWLINE# indexer = NoSearch ( ) #NEWLINE# #UNINDENT# if options . no_cache : #NEWLINE# #INDENT# log . app_log . info ( 'Not using cache' ) #NEWLINE# cache = MockCache ( ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( pylibmc and memcache_urls ) : #NEWLINE# #INDENT# kwargs = dict ( pool = mc_pool ) #NEWLINE# username = os . environ . get ( 'MEMCACHIER_USERNAME' , '' ) #NEWLINE# password = os . environ . get ( 'MEMCACHIER_PASSWORD' , '' ) #NEWLINE# if ( username and password ) : #NEWLINE# #INDENT# kwargs [ 'binary' ] = True #NEWLINE# kwargs [ 'username' ] = username #NEWLINE# kwargs [ 'password' ] = password #NEWLINE# log . app_log . info ( 'Using SASL memcache' ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# log . app_log . info ( 'Using plain memecache' ) #NEWLINE# #UNINDENT# cache = AsyncMultipartMemcache ( memcache_urls . split ( ',' ) , ** kwargs ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# log . app_log . info ( 'Using in-memory cache' ) #NEWLINE# cache = DummyAsyncCache ( ) #NEWLINE# #UNINDENT# #UNINDENT# template_paths = pjoin ( here , 'templates' ) #NEWLINE# if ( options . template_path is not None ) : #NEWLINE# #INDENT# log . app_log . info ( 'Using custom template path {}' . format ( options . template_path ) ) #NEWLINE# template_paths = [ options . template_path , template_paths ] #NEWLINE# #UNINDENT# static_path = pjoin ( here , 'static' ) #NEWLINE# env = Environment ( loader = FileSystemLoader ( template_paths ) , autoescape = True ) #NEWLINE# env . filters [ 'markdown' ] = markdown . markdown #NEWLINE# try : #NEWLINE# #INDENT# git_data = git_info ( here ) #NEWLINE# #UNINDENT# except Exception as e : #NEWLINE# #INDENT# app_log . error ( 'Failed to get git info: %s' , e ) #NEWLINE# git_data = { #NEWLINE# #INDENT# #NEWLINE# #UNINDENT# } #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# git_data [ 'msg' ] = escape ( git_data [ 'msg' ] ) #NEWLINE# #UNINDENT# if options . no_cache : #NEWLINE# #INDENT# env . globals . update ( cache_size = 0 ) #NEWLINE# #UNINDENT# env . globals . update ( nrhead = nrhead , nrfoot = nrfoot , git_data = git_data , jupyter_info = jupyter_info ( ) , len = len ) #NEWLINE# AsyncHTTPClient . configure ( HTTPClientClass ) #NEWLINE# client = AsyncHTTPClient ( ) #NEWLINE# client . cache = cache #NEWLINE# with io . open ( options . frontpage , 'r' ) as f : #NEWLINE# #INDENT# frontpage_sections = json . load ( f ) #NEWLINE# #UNINDENT# max_cache_uris = { '' } #NEWLINE# for section in frontpage_sections : #NEWLINE# #INDENT# for link in section [ 'links' ] : #NEWLINE# #INDENT# max_cache_uris . add ( ( '/' + link [ 'target' ] ) ) #NEWLINE# #UNINDENT# #UNINDENT# fetch_kwargs = dict ( connect_timeout = 10 ) #NEWLINE# if options . proxy_host : #NEWLINE# #INDENT# fetch_kwargs . update ( dict ( proxy_host = options . proxy_host , proxy_port = options . proxy_port ) ) #NEWLINE# log . app_log . info ( 'Using web proxy {proxy_host}:{proxy_port}.' . format ( ** fetch_kwargs ) ) #NEWLINE# #UNINDENT# if options . no_check_certificate : #NEWLINE# #INDENT# fetch_kwargs . update ( dict ( validate_cert = False ) ) #NEWLINE# log . app_log . info ( 'Not validating SSL certificates' ) #NEWLINE# #UNINDENT# settings = dict ( log_function = log_request , jinja2_env = env , static_path = static_path , client = client , formats = tcp_memcache , default_format = options . default_format , providers = options . providers , provider_rewrites = options . provider_rewrites , config = config , index = indexer , cache = cache , cache_expiry_min = options . cache_expiry_min , cache_expiry_max = options . cache_expiry_max , max_cache_uris = max_cache_uris , frontpage_sections = frontpage_sections , pool = pool , gzip = True , render_timeout = options . render_timeout , localfile_path = os . path . abspath ( options . localfiles ) , fetch_kwargs = fetch_kwargs , mathjax_url = options . mathjax_url , statsd_host = options . statsd_host , statsd_port = options . statsd_port , statsd_prefix = options . statsd_prefix ) #NEWLINE# handlers = init_handlers ( formats , options . providers ) #NEWLINE# if options . localfiles : #NEWLINE# #INDENT# log . app_log . warning ( 'Serving local notebooks in %s, this can be a security risk' , options . localfiles ) #NEWLINE# local_handlers = [ ( '/localfile/(.*)' , LocalFileHandler ) ] #NEWLINE# handlers = ( ( local_handlers + format_handlers ( formats , local_handlers ) ) + handlers ) #NEWLINE# #UNINDENT# return web . Application ( handlers , debug = options . debug , ** settings )