fortran66のブログ

fortran について書きます。

【メモ帳】Jupyter, Forpy, Fortran kernel, inlilne の続き

forpy から inline 命令出せない?

IPython.core.getipython.get_ipython().magic('matplotlib inline') によって、実行命令で inline マジックコマンドが発行できるかとも思ったが、出来ない模様。

Module: core.getipython — IPython 6.5.0 documentation

IPython.core.getipython.get_ipython() で戻り値が None になっているので、Jupyter の中で動いているとはいえ、forpy は独立した Python 処理系で動いているのだから、Ipython のインスタンスが取れなくても当然のような気がする。したがって、forpy からの呼び出しで inline 化は出来ないような気がする。
f:id:fortran66:20180915010922p:plain
Python Kernel の場合
f:id:fortran66:20180915135232p:plain

import IPython.core.getipython
ip = IPython.core.getipython.get_ipython()
ip.magic('matplotlib inline')
print(ip)

<ipykernel.zmqshell.ZMQInteractiveShell object at 0x0000020B2527DDD8>

もっとも、CMD 窓の サーバーのメッセージには ZMQ は closed channel にデータ受けたとかメッセージを返しているので、別の手法で乗り越えられるのかもしれない。

画像 inline 表示について

なお、ファイルの画像は MIME type を変えるだけで、png/gif/jpeg/bmp なんでもござれと分かった。拡張子を写せばいいが jpg だけは jpeg

よく分からないが、タグで適切に修飾して send_response というサブルーチンに文字列を渡せば、ブラウザの受信になる模様。ASCII化した画像のみならず平文テキストからなんでも同じ。notebook には、これが溜まってゆくので画像などがあるとサイズが膨らみそう。

IDL kernel から丸写し

        #plot figure 
        tmp = magics['fig']
        if p.returncode == 0 and os.path.exists(tmp[0]):
            _, ext = os.path.splitext(tmp[0])
            ext = ext[1:]
            if ext == 'jpg': # MIME type
                ext = 'jpeg'
            image_type = 'image/' + ext
            image = open(tmp[0], 'rb').read() 
            data = {image_type: b64encode(image).decode('ascii')}
            self.send_response(self.iopub_socket, 'display_data',{'data':data,'metadata':{}})

            if tmp[0] == 'tmp.png':
                os.remove('tmp.png') 

Magic コマンド

coarray fortran kernel から丸写し

    def _filter_magics(self, code):

        magics = {'fcflags': [],
                  'ldflags': [],
                  'module': [],
                  'args': [],
                  'fig': ['tmp.png'],
                 }

        for line in code.splitlines():
            if line.strip().startswith('%'):
                key, value = line.strip().strip('%').split(":", 1)
                key = key.lower()
                
                if key in ['ldflags', 'fcflags', 'args']:
                    magics[key] = shsplit(value)
                elif key in ['module', 'Module']:  
                    magics['module'] = shsplit(value)
                    magics['ldflags'].append('-c')
                elif key in ['fig', 'Fig', 'figure', 'Figure']:  
                    magics['fig'] = shsplit(value)
                else:
                    pass # need to add exception handling
        
        return magics

HTML 部分

本当は carriage control のように先頭1文字目だけで指定したい><;

    def _write_to_stdout(self, contents):
        if contents[0:6] == '<HTML>':
            data = {'text/html': contents}
            self.send_response(self.iopub_socket, 'display_data',{'data':data,'metadata':{}})
        else:      
            self.send_response(self.iopub_socket, 'stream', {'name': 'stdout', 'text': contents})

f:id:fortran66:20180915020651p:plain

Classical Fortran: Programming for Engineering and Scientific Applications, Second Edition

Classical Fortran: Programming for Engineering and Scientific Applications, Second Edition

Modern Fortran Explained: Incorporating Fortran 2018 (Numerical Mathematics and Scientific Computation)

Modern Fortran Explained: Incorporating Fortran 2018 (Numerical Mathematics and Scientific Computation)

Modern Fortran: Style and Usage

Modern Fortran: Style and Usage

Modern Fortran in Practice

Modern Fortran in Practice

Numerical Computing with Modern Fortran (Applied Mathematics)

Numerical Computing with Modern Fortran (Applied Mathematics)

Modern Fortran: Tricks of the Software Engineering Trade (Chapman & Hall/CRC Computational Science)

Modern Fortran: Tricks of the Software Engineering Trade (Chapman & Hall/CRC Computational Science)