Ejemplo n.º 1
0
def generate( context={}, plot_funcs = {}, database_file="database.sqlite", input_output_list=None, open_browser=False ):
    """Generates a html report. Uses jinja2 templating engine with custom
       matplotlib tag, which inserts matplotlib figures as svg graphics"""
    
    def get_db():
        if get_db.databaseHandle is None:
            get_db.databaseHandle = sqlite3.connect( database_file )
        return get_db.databaseHandle
    get_db.databaseHandle = None

    
    context['database_file'] = database_file
    
    loaders = ChoiceLoader( [ FileSystemLoader('.'), 
                              PrefixLoader( { 'waLBerla' : PackageLoader ('waLBerla', 'tools', 'report', 'templates') } ) ] )
    

    env = Environment( loader = loaders, extensions=[MatplotlibExtension] )
    env.database_file   = database_file 
    env.database_getter = get_db
    env.plot_funcs      = plot_funcs

    if input_output_list is None:
        input_output_list = [ ( input, input[2:]) for input in glob( "t_*.html") ] 
        context.update( { 'reports' : [ r[1] for r in input_output_list ] } )
        input_output_list.append( ('waLBerla/report_overview.html', 'index.html') )
        
    for e in input_output_list:
        template = env.get_template( e[0] )
        with open( e[1],"w") as f:
            print( "-- Generating " + e[1] )
            f.write( template.render( **context ) )
            
            
    if open_browser:
        import webbrowser
        webbrowser.open( os.path.join( os.getcwd(),'index.html' ) )