Skip to content

Commit

Permalink
PyScript Integrated Component
Browse files Browse the repository at this point in the history
  • Loading branch information
its-ChaTTy committed Jun 19, 2024
1 parent 912c7df commit 8c90f10
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 2 deletions.
49 changes: 49 additions & 0 deletions _ext/pyscript.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
from docutils import nodes
from docutils.parsers.rst import Directive


class PyScriptDirective(Directive):
has_content = True

def run(self):
self.assert_has_content()
code = '\n'.join(self.content)

# Create the necessary HTML and JavaScript code for the PyScript
html = f'''
<div>
<iframe height="600px" width="100%" srcdoc='
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="https://pyscript.net/latest/pyscript.css" />
<script defer src="https://pyscript.net/latest/pyscript.js"></script>
<style>
.py-repl-run-button{{opacity: 1;}}
.py-repl-output{{margin-top: 1rem;background-color: #000;color: #fff;font-family: monospace;padding: 0.5rem;min-height: 10em;overflow: auto;}}
</style>
</head>
<body>
<py-config>
terminal = false
packages = ["pandas", "matplotlib", "numpy"]
</py-config>
<py-repl id="code">
{code}
</py-repl>
</body>
</html>'
style="border: 8px solid #ccc;border-radius: 10px;"
>
</iframe>
</div>
'''

# Create a raw node that will contain the HTML and JavaScript code
raw_node = nodes.raw('', html, format='html')

return [raw_node]


def setup(app):
app.add_directive('pyscript', PyScriptDirective)
3 changes: 2 additions & 1 deletion _sources/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,5 @@ Contenidos:
:maxdepth: 1

index_es
index_en
index_en
pyComponent
7 changes: 7 additions & 0 deletions _sources/pyComponent.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
-------------------
PyScript Component
-------------------

.. pyscript::

print("Hello, World!")
3 changes: 2 additions & 1 deletion conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
# add these directories to sys.path here. If the directory is relative to the
# documentation root, use os.path.abspath to make it absolute, like shown here.
# sys.path.insert(0, os.path.abspath('../modules'))
sys.path.insert(0, os.path.abspath('_ext'))

from runestone import runestone_static_dirs, runestone_extensions, setup
import pkg_resources
Expand All @@ -28,7 +29,7 @@

# Add any Sphinx extension module names here, as strings. They can be extensions
# coming with Sphinx (named 'sphinx.ext.*') or your custom ones.
extensions = ["sphinx.ext.mathjax"] + runestone_extensions()
extensions = ["sphinx.ext.mathjax", "pyscript"] + runestone_extensions()

# ,'runestone.video','runestone.reveal','runestone.poll','runestone.tabbedStuff','runestone.disqus','runestone.codelens','runestone.activecode', 'runestone.assess', 'runestone.animation','runestone.meta', 'runestone.parsons', 'runestone.blockly', 'runestone.livecode','runestone.accessibility']

Expand Down

0 comments on commit 8c90f10

Please sign in to comment.