Working with subkernels

Unless you would like to start multiple versions of the same kernel or link a language module to a kernel that is not recognized by SoS, you can start a subkernel simply by selecting the language from the language dropdown to the top right corner of each cell.

Sending input to subkernel

The SoS language is based on Python 3.6+ so any python statements are acceptable


In [1]:
filename = '1_Expand_and_Capture.ipynb'

In [2]:
wc -l 1_Expand_and_Capture.ipynb


     352 1_Expand_and_Capture.ipynb

With magic %expand, cell content is expanded as Python f-string before sending to the subkernel


In [3]:
%expand
wc -l {filename}


     352 1_Expand_and_Capture.ipynb

In [4]:
%expand
var fs = require('fs');
JSON.parse(fs.readFileSync("{filename}", 'utf8'));


Out[4]:
{ cells: 
   [ { cell_type: 'markdown', metadata: [Object], source: [Array] },
     { cell_type: 'markdown', metadata: [Object], source: [Array] },
     { cell_type: 'markdown', metadata: [Object], source: [Array] },
     { cell_type: 'markdown', metadata: [Object], source: [Array] },
     { cell_type: 'code',
       execution_count: 1,
       metadata: [Object],
       outputs: [],
       source: [Array] },
     { cell_type: 'code',
       execution_count: 2,
       metadata: [Object],
       outputs: [Array],
       source: [Array] },
     { cell_type: 'markdown', metadata: [Object], source: [Array] },
     { cell_type: 'code',
       execution_count: 3,
       metadata: [Object],
       outputs: [Array],
       source: [Array] },
     { cell_type: 'code',
       execution_count: 4,
       metadata: [Object],
       outputs: [Array],
       source: [Array] },
     { cell_type: 'markdown', metadata: [Object], source: [Array] },
     { cell_type: 'code',
       execution_count: 5,
       metadata: [Object],
       outputs: [Array],
       source: [Array] },
     { cell_type: 'code',
       execution_count: 6,
       metadata: [Object],
       outputs: [Array],
       source: [Array] },
     { cell_type: 'code',
       execution_count: 7,
       metadata: [Object],
       outputs: [Array],
       source: [Array] },
     { cell_type: 'code',
       execution_count: 8,
       metadata: [Object],
       outputs: [Array],
       source: [Array] } ],
  metadata: 
   { kernelspec: { display_name: 'SoS', language: 'sos', name: 'sos' },
     language_info: 
      { codemirror_mode: 'sos',
        file_extension: '.sos',
        mimetype: 'text/x-sos',
        name: 'sos',
        nbconvert_exporter: 'sos_notebook.converter.SoS_Exporter',
        pygments_lexer: 'sos' },
     sos: { kernels: [Array], panel: [Object], version: '0.9.16.7' } },
  nbformat: 4,
  nbformat_minor: 2 }

Capture output from subkernels


In [5]:
%capture --to res
%expand
wc -l {filename}


     352 1_Expand_and_Capture.ipynb

In [6]:
res


Out[6]:
'     352 1_Expand_and_Capture.ipynb\n'

In [7]:
%capture html --to result

%endpoint http://dbpedia.org/sparql
%display table 
SELECT DISTINCT ?property
WHERE {
   ?s ?property ?person .
   ?person rdf:type foaf:Person .
}
LIMIT 3


Endpoint set to: http://dbpedia.org/sparql
Display: table

In [8]:
from bs4 import BeautifulSoup as Soup
html = Soup(result, 'html.parser') 
[a['href'] for a in html.find_all('a')]


Out[8]:
['http://www.w3.org/2002/07/owl#differentFrom',
 'http://www.w3.org/2000/01/rdf-schema#seeAlso',
 'http://www.w3.org/2002/07/owl#sameAs']