<s> import os , shutil , string , glob , io <EOL> from sphinx . util . compat import Directive <EOL> from docutils . parsers . rst import directives <EOL> from IPython . nbconvert import html , python <EOL> from IPython . nbformat import current <EOL> from runipy . notebook_runner import NotebookRunner <EOL> from jinja2 import FileSystemLoader <EOL> from notebook_sphinxext import notebook_node , nb_to_html , nb_to_python , visit_notebook_node , depart_notebook_node , evaluate_notebook <EOL> class NotebookCellDirective ( Directive ) : <EOL> """<STR_LIT>""" <EOL> required_arguments = <NUM_LIT:0> <EOL> optional_arguments = <NUM_LIT:1> <EOL> has_content = True <EOL> option_spec = { '<STR_LIT>' : directives . flag } <EOL> def run ( self ) : <EOL> if not self . state . document . settings . raw_enabled : <EOL> raise self . warning ( '<STR_LIT>' % self . name ) <EOL> content = "<STR_LIT:\n>" . join ( self . content ) <EOL> with open ( "<STR_LIT>" , "<STR_LIT:w>" ) as f : <EOL> f . write ( content ) <EOL> convert_to_ipynb ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> skip_exceptions = '<STR_LIT>' in self . options <EOL> evaluated_text = evaluate_notebook ( '<STR_LIT>' , skip_exceptions = skip_exceptions ) <EOL> attributes = { '<STR_LIT>' : '<STR_LIT:html>' , '<STR_LIT:source>' : '<STR_LIT>' } <EOL> nb_node = notebook_node ( '<STR_LIT>' , evaluated_text , ** attributes ) <EOL> ( nb_node . source , nb_node . line ) = self . state_machine . get_source_and_line ( self . lineno ) <EOL> files = glob . glob ( "<STR_LIT>" ) + [ '<STR_LIT>' , '<STR_LIT>' ] <EOL> for file in files : <EOL> os . remove ( file ) <EOL> return [ nb_node ] <EOL> def setup ( app ) : <EOL> setup . app = app <EOL> setup . config = app . config <EOL> setup . confdir = app . confdir <EOL> app . add_node ( notebook_node , <EOL> html = ( visit_notebook_node , depart_notebook_node ) ) <EOL> app . add_directive ( '<STR_LIT>' , NotebookCellDirective ) <EOL> def convert_to_ipynb ( py_file , ipynb_file ) : <EOL> with io . open ( py_file , '<STR_LIT:r>' , encoding = '<STR_LIT:utf-8>' ) as f : <EOL> notebook = current . reads ( f . read ( ) , format = '<STR_LIT>' ) <EOL> with io . open ( ipynb_file , '<STR_LIT:w>' , encoding = '<STR_LIT:utf-8>' ) as f : <EOL> current . write ( notebook , f , format = '<STR_LIT>' ) </s>
