Ejemplo n.º 1
0
    def run(self):

        import os
        import shutil
        import tempfile

        from IPython.nbconvert.nbconvertapp import NbConvertApp

        import sys
        for arg in range(len(sys.argv[1:])):
            sys.argv.pop(-1)

        # First convert the lecture notes to slides - we have to do them
        # individually in order to be able to specify a custom output prefix for
        # each.

        app = NbConvertApp()
        app.initialize()
        app.export_format = 'slides'
        app.config.Exporter.template_file = 'config/slides.tpl'
        for notebook in glob.glob('lectures/*.ipynb'):
            app.notebooks = [notebook]
            app.output_base = notebook.replace('.ipynb', '')
            app.start()

        # Now convert the lecture notes, problem sets, and practice problems to
        # HTML notebooks.

        app = NbConvertApp()
        app.initialize()
        app.export_format = 'html'
        for notebook in glob.glob('lectures/*.ipynb'):
            app.notebooks = [notebook]
            app.output_base = notebook.replace('.ipynb', '')
            app.start()
Ejemplo n.º 2
0
    def run(self):

        import os
        import sys

        from IPython.nbconvert.nbconvertapp import NbConvertApp

        self.reinitialize_command('run', inplace=True)
        self.run_command('run')

        for arg in range(len(sys.argv[1:])):
            sys.argv.pop(-1)

        if not os.path.exists(os.path.join('www', '_static')):
            os.mkdir(os.path.join('www', '_static'))

        # Now convert the lecture notes, problem sets, and practice problems to
        # HTML notebooks.

        app = NbConvertApp()
        app.initialize()
        app.export_format = 'html'
        for notebook in glob.glob('?.???/*.ipynb'):
            print("Rendering {0}...".format(notebook))
            app.notebooks = [notebook]
            app.output_base = os.path.join(
                '..', 'www', '_static',
                os.path.basename(notebook.replace('.ipynb', '')))
            app.start()

        data_dir = os.path.join('www', '_static', 'data')
        if not os.path.exists(data_dir):
            os.mkdir(data_dir)
Ejemplo n.º 3
0
    def run(self):

        import os
        import shutil
        import tempfile

        from IPython.nbconvert.nbconvertapp import NbConvertApp

        import sys
        for arg in range(len(sys.argv[1:])):
            sys.argv.pop(-1)

        # Convert the notebooks to HTML notebooks.

        app = NbConvertApp()
        app.initialize()
        app.export_format = 'html'
        for notebook in (glob.glob('notebooks/*.ipynb')):
            app.notebooks = [notebook]
            app.output_base = notebook.replace('.ipynb', '')
            app.start()

        # Make an index of all notes
        f = open('notebooks.html', 'w')
        f.write("<html>\n  <body>\n")

        f.write("    <h1>Solutions:</h1>\n")
        f.write("    <ul>\n")
        for page in glob.glob('notebooks/*.html'):
            f.write('      <li><a href="{0}">{1}</a></li>\n'.format(page, os.path.basename(page).replace('.html', '')))
        f.write('    </ul>\n')

        f.write('  </body>\n</html>')
        f.close()
Ejemplo n.º 4
0
    def run(self):

        import os
        import sys

        from IPython.nbconvert.nbconvertapp import NbConvertApp

        self.reinitialize_command('run', inplace=True)
        self.run_command('run')

        for arg in range(len(sys.argv[1:])):
            sys.argv.pop(-1)

        if not os.path.exists(os.path.join('www', '_static')):
            os.mkdir(os.path.join('www', '_static'))

        # Now convert the lecture notes, problem sets, and practice problems to
        # HTML notebooks.

        app = NbConvertApp()
        app.initialize()
        app.export_format = 'html'
        for notebook in glob.glob('?.???/*.ipynb'):
            print("Rendering {0}...".format(notebook))
            app.notebooks = [notebook]
            app.output_base = os.path.join('..', 'www', '_static', os.path.basename(notebook.replace('.ipynb', '')))
            app.start()

        data_dir = os.path.join('www', '_static', 'data')
        if not os.path.exists(data_dir):
            os.mkdir(data_dir)
Ejemplo n.º 5
0
    def run(self):
        """ Build the tutorials (iPython notebook files) located in tutorials/*
            into static HTML pages.
        """
        from IPython.nbconvert.nbconvertapp import NbConvertApp

        check_ipython_version()

        current_directory = os.getcwd()
        html_base = os.path.join(current_directory,"html")
        if not os.path.exists(html_base):
            os.mkdir(html_base)
        tutorials_base = os.path.join(current_directory,'tutorials')

        app = NbConvertApp()
        app.initialize()
        app.export_format = 'html'

        template_path = os.path.join(tutorials_base, 'templates')
        app.config.Exporter.template_path = ['templates', template_path]
        app.config.Exporter.template_file = 'astropy'

        # walk through each directory in tutorials/ to find all .ipynb file
        index_list = []
        for tutorial_name in os.listdir(tutorials_base):
            path = os.path.join(tutorials_base, tutorial_name)
            if not os.path.isdir(path):
                continue

            # read metadata from config file
            config = SafeConfigParser()
            config.read(os.path.join(path,"metadata.cfg"))

            is_published = config.getboolean("config", "published")
            if not is_published:
                continue

            for filename in os.listdir(path):
                base,ext = os.path.splitext(filename)
                if ext.lower() == ".ipynb" and "checkpoint" not in base:
                    app.output_base = os.path.join(html_base,base)
                    app.notebooks = [os.path.join(path,filename)]
                    app.start()

                    index_listing = dict()
                    index_listing["link_path"] = "{}.html".format(base)
                    index_listing["link_name"] = config.get("config", "link_name")
                    index_list.append(index_listing)

        # Make an index of all notes
        entries = []
        for page in index_list:
            entries.append('      <li><a href="{0[link_path]}">{0[link_name]}</a></li>'.format(page))
        with open(os.path.join(current_directory,'html','index.html'), 'w') as f:
            f.write(INDEX_TEMPLATE.format(entries='\n'.join(entries)))
Ejemplo n.º 6
0
    def run(self):

        for arg in range(len(sys.argv[1:])):
            sys.argv.pop(-1)

        # First convert the lecture notes to slides - we have to do them
        # individually in order to be able to specify a custom output prefix for
        # each.

        app = NbConvertApp()
        app.initialize()
        app.export_format = 'slides'
        app.config.Exporter.template_file = 'output_toggle.tpl'
        # remote version
        app.config.Exporter.reveal_prefix = "//cdn.jsdelivr.net/reveal.js/2.4.0/"
        # local version
        app.config.Exporter.reveal_prefix = '' # 
        for notebook in glob.glob('*.ipynb'):
            app.notebooks = [notebook]
            app.output_base = notebook.replace('.ipynb', '')
            app.start()

            outfile = "build/{}.slides.html".format(app.output_base)
            if os.path.exists(outfile):
                os.remove(outfile)
            shutil.move(app.output_base+".slides.html",outfile)

        # Now convert the lecture notes, problem sets, and practice problems to
        # HTML notebooks.

        app = NbConvertApp()
        app.initialize()
        app.export_format = 'html'
        for notebook in glob.glob('*.ipynb'):
            app.notebooks = [notebook]
            app.output_base = notebook.replace('.ipynb', '')
            app.start()

            outfile = "build/{}.html".format(app.output_base)
            if os.path.exists(outfile):
                os.remove(outfile)
            shutil.move(app.output_base+".html",outfile)
Ejemplo n.º 7
0
    def run(self):

        for arg in range(len(sys.argv[1:])):
            sys.argv.pop(-1)

        # First convert the lecture notes to slides - we have to do them
        # individually in order to be able to specify a custom output prefix for
        # each.

        app = NbConvertApp()
        app.initialize()
        app.export_format = 'slides'
        app.config.Exporter.template_file = 'output_toggle.tpl'
        # remote version
        app.config.Exporter.reveal_prefix = "//cdn.jsdelivr.net/reveal.js/2.4.0/"
        # local version
        app.config.Exporter.reveal_prefix = ''  #
        for notebook in glob.glob('*.ipynb'):
            app.notebooks = [notebook]
            app.output_base = notebook.replace('.ipynb', '')
            app.start()

            outfile = "build/{}.slides.html".format(app.output_base)
            if os.path.exists(outfile):
                os.remove(outfile)
            shutil.move(app.output_base + ".slides.html", outfile)

        # Now convert the lecture notes, problem sets, and practice problems to
        # HTML notebooks.

        app = NbConvertApp()
        app.initialize()
        app.export_format = 'html'
        for notebook in glob.glob('*.ipynb'):
            app.notebooks = [notebook]
            app.output_base = notebook.replace('.ipynb', '')
            app.start()

            outfile = "build/{}.html".format(app.output_base)
            if os.path.exists(outfile):
                os.remove(outfile)
            shutil.move(app.output_base + ".html", outfile)
Ejemplo n.º 8
0
def convert_notebooks(selected_nb_re=None):
    """ Convert the tutorials (IPython notebook files) located in tutorials/*
        into static HTML pages.
    """
    from IPython.nbconvert.nbconvertapp import NbConvertApp

    current_directory = os.getcwd()
    html_base = os.path.join(current_directory,"html")
    if not os.path.exists(html_base):
        os.mkdir(html_base)
    template_path = os.path.join(current_directory, 'templates')

    app = NbConvertApp()
    app.initialize(argv=[]) # hack
    app.export_format = 'html'
    app.config.Exporter.template_path = ['templates', template_path]
    app.config.Exporter.template_file = 'astropy'

    # walk through each directory in tutorials/ to find all .ipynb file
    index_list = []
    re_str = ('_run_' + selected_nb_re) if selected_nb_re else None
    for tutorial_filename,nb in walk_through_tutorials(only_published=True,
                                selected_nb_re=re_str):
        path,filename = os.path.split(tutorial_filename)
        if not filename.startswith("_run_"):
            continue

        # remove _run_ from base filename
        base = os.path.splitext(filename)[0]
        cleanbase = base.lstrip("_run_")

        app.output_base = os.path.join(html_base,cleanbase)
        app.notebooks = [os.path.join(path,filename)]
        app.start()

        index_listing = dict()
        index_listing["link_path"] = "{}.html".format(cleanbase)
        index_listing["link_name"] = nb['metadata']['astropy-tutorials']['link_name']
        index_list.append(index_listing)

    # Make an index of all notes
    entries = []
    for page in sorted(index_list, key=lambda x: x['link_name']): # sort on tutorial name
        entries.append('      <li><a href="{0[link_path]}">{0[link_name]}</a></li>'.format(page))

    with open(os.path.join(current_directory,'html','index.html'), 'w') as f:
        f.write(INDEX_TEMPLATE.format(entries='\n'.join(entries)))
Ejemplo n.º 9
0
    def run(self):

        import os
        import shutil
        import tempfile

        from IPython.nbconvert.nbconvertapp import NbConvertApp

        import sys
        for arg in range(len(sys.argv[1:])):
            sys.argv.pop(-1)

        # First convert the lecture notes to slides - we have to do them
        # individually in order to be able to specify a custom output prefix for
        # each.

        app = NbConvertApp()
        app.initialize()
        app.export_format = 'slides'
        app.config.Exporter.template_file = 'lectures/py4sci_template.tpl'
        for notebook in glob.glob('lectures/*.ipynb'):
            app.notebooks = [notebook]
            app.output_base = notebook.replace('.ipynb', '')
            app.start()

        # Now convert the lecture notes, problem sets, and practice problems to
        # HTML notebooks.

        app = NbConvertApp()
        app.initialize()
        app.export_format = 'html'
        for notebook in (glob.glob('lectures/*.ipynb') +
                         glob.glob('problems/*.ipynb') +
                         glob.glob('practice/*.ipynb')):
            app.notebooks = [notebook]
            app.output_base = notebook.replace('.ipynb', '')
            app.start()

        # Make an index of all notes
        f = open('index.html', 'w')
        f.write("<html>\n  <body>\n")

        f.write("    <h1>Lectures:</h1>\n")
        f.write("    <ul>\n")
        for page in glob.glob('lectures/*.html'):
            f.write('      <li><a href="{0}">{1}</a></li>\n'.format(
                page,
                os.path.basename(page).replace('.html', '')))
        f.write('    </ul>\n')

        f.write("    <h1>Problem Sets:</h1>\n")
        f.write("    <ul>\n")
        for page in glob.glob('problems/*.html'):
            f.write('      <li><a href="{0}">{1}</a></li>\n'.format(
                page,
                os.path.basename(page).replace('.html', '')))
        f.write('    </ul>\n')

        f.write("    <h1>Practice Sheets:</h1>\n")
        f.write("    <ul>\n")
        for page in glob.glob('practice/*.html'):
            f.write('      <li><a href="{0}">{1}</a></li>\n'.format(
                page,
                os.path.basename(page).replace('.html', '')))
        f.write('    </ul>\n')

        f.write('  </body>\n</html>')
        f.close()
Ejemplo n.º 10
0
    def run(self):

        import os
        import shutil
        import tempfile

        from IPython.nbconvert.nbconvertapp import NbConvertApp

        import sys
        for arg in range(len(sys.argv[1:])):
            sys.argv.pop(-1)

        # First convert the lecture notes to slides - we have to do them
        # individually in order to be able to specify a custom output prefix for
        # each.

        app = NbConvertApp()
        app.initialize()
        app.export_format = 'slides'
        app.config.Exporter.template_file = 'lectures/py4sci_template.tpl'
        for notebook in glob.glob('lectures/*.ipynb'):
            app.notebooks = [notebook]
            app.output_base = notebook.replace('.ipynb', '')
            app.start()

        # Now convert the lecture notes, problem sets, and practice problems to
        # HTML notebooks.

        app = NbConvertApp()
        app.initialize()
        app.export_format = 'html'
        for notebook in (glob.glob('lectures/*.ipynb')
                        + glob.glob('problems/*.ipynb')
                        + glob.glob('practice/*.ipynb')):
            app.notebooks = [notebook]
            app.output_base = notebook.replace('.ipynb', '')
            app.start()

        # Make an index of all notes
        f = open('index.html', 'w')
        f.write("<html>\n  <body>\n")

        f.write("    <h1>Lectures:</h1>\n")
        f.write("    <ul>\n")
        for page in glob.glob('lectures/*.html'):
            f.write('      <li><a href="{0}">{1}</a></li>\n'.format(page, os.path.basename(page).replace('.html', '')))
        f.write('    </ul>\n')

        f.write("    <h1>Problem Sets:</h1>\n")
        f.write("    <ul>\n")
        for page in glob.glob('problems/*.html'):
            f.write('      <li><a href="{0}">{1}</a></li>\n'.format(page, os.path.basename(page).replace('.html', '')))
        f.write('    </ul>\n')

        f.write("    <h1>Practice Sheets:</h1>\n")
        f.write("    <ul>\n")
        for page in glob.glob('practice/*.html'):
            f.write('      <li><a href="{0}">{1}</a></li>\n'.format(page, os.path.basename(page).replace('.html', '')))
        f.write('    </ul>\n')

        f.write('  </body>\n</html>')
        f.close()
Ejemplo n.º 11
0
def convert_notebooks(selected_nb_re=None):
    """ Convert the tutorials (IPython notebook files) located in tutorials/*
        into static HTML pages.
    """
    from IPython.nbconvert.nbconvertapp import NbConvertApp

    current_directory = os.getcwd()
    html_base = os.path.join(current_directory,"html")
    if not os.path.exists(html_base):
        os.mkdir(html_base)
    template_path = os.path.join(current_directory, 'templates')

    app = NbConvertApp()
    app.initialize(argv=[]) # hack
    app.export_format = 'html'
    app.config.Exporter.template_path = ['templates', template_path]
    app.config.Exporter.template_file = 'astropy'

    # walk through each directory in tutorials/ to find all .ipynb file
    index_list = []
    re_str = ('_run_' + selected_nb_re) if selected_nb_re else None
    for tutorial_filename,nb in walk_through_tutorials(only_published=True,
                                selected_nb_re=re_str):
        path,filename = os.path.split(tutorial_filename)
        if not filename.startswith("_run_"):
            continue

        # remove _run_ from base filename
        base = os.path.splitext(filename)[0]
        cleanbase = base.lstrip("_run_")

        app.output_base = os.path.join(html_base,cleanbase)
        app.notebooks = [os.path.join(path,filename)]
        app.start()

        # HACK to set title and other things in rendered notebook
        html_filename = "{}.html".format(os.path.join(html_base,cleanbase))
        with open(html_filename, 'rb') as f:
            html_file_data = f.read()

        with open(html_filename, 'wb') as f:
            html_file_data = html_file_data.decode("utf-8")
            html_file_data = html_file_data.replace("{title}",
                                nb['metadata']['astropy-tutorials']['link_name'])
            f.write(html_file_data.encode("utf8"))

        index_listing = dict()
        index_listing["link_path"] = "{}.html".format(cleanbase)
        index_listing["link_name"] = nb['metadata']['astropy-tutorials']['link_name']
        index_listing["description"] = nb['metadata']['astropy-tutorials']['description']
        index_list.append(index_listing)

    # Make an index of all notes
    entries = []
    for page in sorted(index_list, key=lambda x: x['link_name']): # sort on tutorial name
        entry_html = "<li>"
        entry_html += "<a href='{0[link_path]}''>{0[link_name]}</a>".format(page)
        entry_html += "<br/><span>{0[description]}</span>".format(page)
        entry_html += "</li>"
        entries.append(entry_html)

    with open(os.path.join(current_directory,'html','index.html'), 'w') as f:
        f.write(INDEX_TEMPLATE.format(entries='\n'.join(entries)))
Ejemplo n.º 12
0
def convert_notebooks(selected_nb_re=None):
    """ Convert the tutorials (IPython notebook files) located in tutorials/*
        into static HTML pages.
    """
    from IPython.nbconvert.nbconvertapp import NbConvertApp

    current_directory = os.getcwd()
    html_base = os.path.join(current_directory,"html")
    if not os.path.exists(html_base):
        os.mkdir(html_base)
    template_path = os.path.join(current_directory, 'templates')

    app = NbConvertApp()
    app.initialize(argv=[]) # hack
    app.export_format = 'html'
    app.config.Exporter.template_path = ['templates', template_path]
    app.config.Exporter.template_file = 'astropy'

    # walk through each directory in tutorials/ to find all .ipynb file
    index_list = []
    re_str = ('_run_' + selected_nb_re) if selected_nb_re else None
    for tutorial_filename,nb in walk_through_tutorials(only_published=True,
                                selected_nb_re=re_str):
        path,filename = os.path.split(tutorial_filename)
        if not filename.startswith("_run_"):
            continue

        # remove _run_ from base filename
        base = os.path.splitext(filename)[0]
        cleanbase = base.lstrip("_run_")

        app.output_base = os.path.join(html_base,cleanbase)
        app.notebooks = [os.path.join(path,filename)]
        app.start()

        # HACK to set title and other things in rendered notebook
        html_filename = "{}.html".format(os.path.join(html_base,cleanbase))
        with open(html_filename, 'rb') as f:
            html_file_data = f.read()

        with open(html_filename, 'wb') as f:
            html_file_data = html_file_data.decode("utf-8")
            html_file_data = html_file_data.replace("{title}",nb['metadata']['astropy-tutorials']['link_name'])
            html_file_data = html_file_data.replace("{author}",nb['metadata']['astropy-tutorials']['author'])
            html_file_data = html_file_data.replace("{date}",nb['metadata']['astropy-tutorials']['date'])
            html_file_data = html_file_data.replace("{abspageurl}","{}".format(cleanbase))
            html_file_data = html_file_data.replace("{pageurl}","{}.html".format(cleanbase))
            f.write(html_file_data.encode("utf8"))

        index_listing = dict()
        index_listing["link_path"] = "{}.html".format(cleanbase)
        index_listing["link_name"] = nb['metadata']['astropy-tutorials']['link_name']
        index_listing["description"] = nb['metadata']['astropy-tutorials']['description']
        index_list.append(index_listing)

    # Make an index of all notes
    entries = []
    for page in sorted(index_list, key=lambda x: x['link_name']): # sort on tutorial name
        entry_html = "<li>"
        entry_html += "<a href='{0[link_path]}''>{0[link_name]}</a>".format(page)
        entry_html += "<br/><span>{0[description]}</span>".format(page)
        entry_html += "</li>"
        entries.append(entry_html)

    with open(os.path.join(current_directory,'html','index.html'), 'w') as f:
        f.write(INDEX_TEMPLATE.format(entries='\n'.join(entries)))
Ejemplo n.º 13
0
def convert_notebooks(selected_nb_re=None):
    """ Convert the tutorials (IPython notebook files) located in tutorials/*
        into static HTML pages.
    """
    from IPython.nbconvert.nbconvertapp import NbConvertApp

    current_directory = os.getcwd()
    html_base = os.path.join(current_directory,"html")
    if not os.path.exists(html_base):
        os.mkdir(html_base)
    template_path = os.path.join(current_directory, 'templates')

    app = NbConvertApp()
    app.initialize(argv=[]) # hack
    app.export_format = 'html'
    app.config.Exporter.template_path = ['templates', template_path]
    app.config.Exporter.template_file = 'sunpy'


    # Construct a JSON representation of all the tutorials in the gallery
    gallery = {}

    gallery['meta'] = {
        'sunpy': sunpy.__version__
    }
    gallery['sections'] = {}

    # walk through each directory in tutorials/ to find all .ipynb file
    re_str = ('_run_' + selected_nb_re) if selected_nb_re else None
    for tutorial_filename,nb in walk_through_tutorials(only_published=True,
                                selected_nb_re=re_str):
        path,filename = os.path.split(tutorial_filename)
        if not filename.startswith("_run_"):
            continue

        # remove _run_ from base filename
        base = os.path.splitext(filename)[0]
        cleanbase = base.lstrip("_run_")

        section_name = nb['metadata']['sunpy-gallery'].get('section_name', 'Examples')
        section_dir = os.path.join(html_base, section_name)
        if not os.path.exists(section_dir):
            os.makedirs(section_dir)

        app.output_base = os.path.join(section_dir, cleanbase)
        app.notebooks = [os.path.join(path,filename)]
        app.start()



        # If we haven't processed this section before make it now
        if gallery['sections'].get(section_name, None) is None:
            gallery['sections'][section_name] = []

        gallery['sections'][section_name].append({'notebook': "{}".format(cleanbase),
                                                  'link_name': nb['metadata']['sunpy-gallery']['link_name']})

        # Extract a tumbnail and save it to the same filename as the notebook
        # with a different extension.
        target_file = os.path.join(section_dir, cleanbase)
        extract_notebook_thumbnail(os.path.join(section_dir, tutorial_filename), target_file)

    # Save index_template to index.html
    with open(os.path.join(current_directory,'html','index.html'), 'w') as f:
        f.write(INDEX_TEMPLATE)

    # Output gallery as JSON
    gallery_json = json.dumps(gallery)

    with open('html/gallery.json', 'w') as fp:
        fp.write(gallery_json)