Skip to content

Instantly share code, notes, and snippets.

@kapulkin
Last active March 5, 2021 13:56
Show Gist options
  • Save kapulkin/d79ed647fc075b14b9cd4ca67cc657bb to your computer and use it in GitHub Desktop.
Save kapulkin/d79ed647fc075b14b9cd4ca67cc657bb to your computer and use it in GitHub Desktop.

Revisions

  1. kapulkin revised this gist Nov 8, 2019. 2 changed files with 10 additions and 1 deletion.
    8 changes: 7 additions & 1 deletion pyodide.html
    Original file line number Diff line number Diff line change
    @@ -30,7 +30,13 @@

    pyodide.loadPackage(['numpy']).then(() => {
    const el = document.getElementById("pyodide")
    const data = pyodide.runPython(`import numpy as np\nimport pyodide\ndata = pyodide.open_url("${fileUrl}").read()\nnp.array(data).tolist()`)
    pyodide.runPython(
    `import numpy as np
    import pyodide
    data = pyodide.open_url("${fileUrl}")
    x = np.genfromtxt(data, delimiter=",")
    dataList=(-x).tolist()`)
    const data = pyodide.pyimport("dataList")
    el.textContent = data
    console.log(data)
    })
    3 changes: 3 additions & 0 deletions test.csv
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,3 @@
    1725, 1747
    1753, 1775
    1007, 1029
  2. kapulkin revised this gist Nov 8, 2019. 1 changed file with 0 additions and 2 deletions.
    2 changes: 0 additions & 2 deletions pyodide.html
    Original file line number Diff line number Diff line change
    @@ -18,8 +18,6 @@
    }
    }

    let dataForPython

    function handleFiles (files) {
    const file = files[0]
    const fileUrl = createObjectURL(file)
  3. kapulkin created this gist Nov 7, 2019.
    60 changes: 60 additions & 0 deletions pyodide.html
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,60 @@
    <html>
    <head>

    </head>
    <input type="file" id="fileInput" onchange="handleFiles(this.files)"/>
    <div id="pyodide"/>
    <body>
    <script type="text/javascript">
    self.languagePluginUrl = "/python/"

    function createObjectURL ( file ) {
    if ( window.webkitURL ) {
    return window.webkitURL.createObjectURL( file );
    } else if ( window.URL && window.URL.createObjectURL ) {
    return window.URL.createObjectURL( file );
    } else {
    return null;
    }
    }

    let dataForPython

    function handleFiles (files) {
    const file = files[0]
    const fileUrl = createObjectURL(file)
    console.log(fileUrl)

    downloadPyodide(function () {
    const el = document.getElementById("pyodide")
    console.log('py')
    el.textContent = pyodide.runPython('import sys\nsys.version')

    pyodide.loadPackage(['numpy']).then(() => {
    const el = document.getElementById("pyodide")
    const data = pyodide.runPython(`import numpy as np\nimport pyodide\ndata = pyodide.open_url("${fileUrl}").read()\nnp.array(data).tolist()`)
    el.textContent = data
    console.log(data)
    })
    })

    }

    function downloadPyodide (callback) {
    const onScriptLoad = function () {
    languagePluginLoader.then(function () {
    callback(pyodide)
    })
    }

    const scriptElement = document.createElement('script')
    scriptElement.type = 'text/javascript'
    scriptElement.src = '/python/pyodide.js'
    scriptElement.onreadystatechange = onScriptLoad
    scriptElement.onload = onScriptLoad

    document.head.appendChild(scriptElement)
    }
    </script>
    </body>
    </html>