packages = ("package","another-package")
It is important to note that not all PyPI packages can be installed and executed. Most “pure” Python packages (approximately humanize) should run smoothly, just like numpy, pandas, bokeh or matplotlib. The situation is different for packages that require network access or work with platform-specific elements such as GUIs. They most likely won’t work.
Import locally
Another common scenario: importing other Python scripts that are in the same directory tree as your website. Using imports makes it easier to offload more Python logic from the website itself. Because there it is “mixed” with your presentation, which could make it difficult to work with.
Normally Python uses other existing ones .py-Files in the file system to show what it can import. PyScript doesn’t work this way. Therefore, you must specify which files should be available as importable modules.
To do this, list the corresponding URLs in your application’s configuration file in a (files) block and define how they should be mapped to PyScript’s emulated file system. For example:
(files)"/module.py" = "./libs/module.py""https://mydata.com/data.csv" = "./data.csv"
Each file accessible via the URL on the left is made available to the Python interpreter’s emulated file system via the path on the right. In this case, the file you would see if you /module.py would call, for Python as libs.module available. The file is also at the URL https://mydata.com/data.csv available in the emulated current working directory.
The in-browser terminal
Python users should be familiar with REPL, the console interface to the Python runtime environment. In PyScript you can embed these into a live terminal in the browser – or just the console output of your Python program.
To embed a terminal, use a script tag that identifies terminal as an attribute:
For interactivity you need the attribute worker use:
name = input("What is your name? ")print(f"Hello, {name}")
Der worker runs your program in a web worker, which is essentially a subprocess. One thing to note here is that web workers cannot be used on a locally loaded HTML file - you have to load it from a web server that provides specific headers. You can read how this works in detail in the PyScript documentation.
In a PyScript terminal you have mostly the same options as in a conventional console - including coloring and Unicode.
Interact with DOM and JavaScript
Because PyScript is based on browser technology, it has DOM interaction mechanisms. For example, if you want to get the value of an input field on a web page and use it in your Python code, do the following:
from pyscript import window, documentinputbox = document.querySelector("#my-input")print("Value of input:", inputbox.value)
PyScript also includes a module called pydomwhich allows objects to be created dynamically on the page:
from pyweb import pydomnew_div = pydom.create("div")new_div.content = "Hello World"
This creates a new one divelement on the page and fills it with text. With the pydomlibrary, most customizations possible with DOM in JavaScript are feasible. (fm)
This article originally appeared at our sister publication Infoworld.com.
