Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1,287 changes: 0 additions & 1,287 deletions docs/api.md

This file was deleted.

3 changes: 3 additions & 0 deletions docs/api/context.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# `pyscript.context`

::: pyscript.context
3 changes: 3 additions & 0 deletions docs/api/display.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# `pyscript.display`

::: pyscript.display
3 changes: 3 additions & 0 deletions docs/api/events.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# `pyscript.event`

::: pyscript.events
3 changes: 3 additions & 0 deletions docs/api/fetch.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# `pyscript.fetch`

::: pyscript.fetch
3 changes: 3 additions & 0 deletions docs/api/ffi.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# `pyscript.ffi`

::: pyscript.ffi
3 changes: 3 additions & 0 deletions docs/api/flatted.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# `pyscript.flatted`

::: pyscript.flatted
3 changes: 3 additions & 0 deletions docs/api/fs.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# `pyscript.fs`

::: pyscript.fs
14 changes: 14 additions & 0 deletions docs/api/init.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# The `pyscript` API

!!! important

These API docs are auto-generated from our source code. To suggest
changes or report errors, please do so via
[our GitHub repository](https://github.com/pyscript/pyscript). The
source code for these APIs
[is found here](https://github.com/pyscript/pyscript/tree/main/core/src/stdlib/pyscript)
in our repository.

::: pyscript
options:
show_root_heading: false
3 changes: 3 additions & 0 deletions docs/api/media.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# `pyscript.media`

::: pyscript.media
3 changes: 3 additions & 0 deletions docs/api/storage.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# `pyscript.storage`

::: pyscript.storage
3 changes: 3 additions & 0 deletions docs/api/util.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# `pyscript.util`

::: pyscript.util
18 changes: 18 additions & 0 deletions docs/api/web.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# `pyscript.web`

::: pyscript.web
options:
members:
- page
- Element
- ContainerElement
- ElementCollection
- Classes
- Style
- HasOptions
- Options
- Page
- canvas
- video
- CONTAINER_TAGS
- VOID_TAGS
3 changes: 3 additions & 0 deletions docs/api/websocket.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# `pyscript.websocket`

::: pyscript.websocket
3 changes: 3 additions & 0 deletions docs/api/workers.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# `pyscript.workers`

::: pyscript.workers
28 changes: 27 additions & 1 deletion mkdocs.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
site_name: PyScript
site_author: The PyScript OSS Team
site_description: PyScript - an open source platform for Python in the browser.

theme:
name: material
Expand Down Expand Up @@ -58,6 +60,16 @@ plugins:
css_dir: css
javascript_dir: js
canonical_version: null
- mkdocstrings:
default_handler: python
locale: en
handlers:
python:
options:
show_source: true
members_order: source
show_symbol_type_heading: true
show_symbol_type_toc: true

nav:
- Home: index.md
Expand All @@ -80,7 +92,21 @@ nav:
- PyGame-CE: user-guide/pygame-ce.md
- Plugins: user-guide/plugins.md
- Use Offline: user-guide/offline.md
- Built-in APIs: api.md
- PyScript APIs:
- Introduction: api/init.md
- context: api/context.md
- display: api/display.md
- events: api/events.md
- fetch: api/fetch.md
- ffi: api/ffi.md
- flatted: api/flatted.md
- fs: api/fs.md
- media: api/media.md
- storage: api/storage.md
- util: api/util.md
- web: api/web.md
- websocket: api/websocket.md
- workers: api/workers.md
- FAQ: faq.md
- Contributing: contributing.md
- Developer Guide: developers.md
Expand Down
120 changes: 120 additions & 0 deletions pyscript/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
"""
This is the main `pyscript` namespace. It provides the primary Pythonic API
for users to interact with the
[browser's own API](https://developer.mozilla.org/en-US/docs/Web/API). It
includes utilities for common activities such as displaying content, handling
events, fetching resources, managing local storage, and coordinating with
web workers.

The most important names provided by this namespace can be directly imported
from `pyscript`, for example:

```python
from pyscript import display, HTML, fetch, when, storage, WebSocket
```

The following names are available in the `pyscript` namespace:

- `RUNNING_IN_WORKER`: Boolean indicating if the code is running in a Web
Worker.
- `PyWorker`: Class for creating Web Workers running Python code.
- `config`: Configuration object for pyscript settings.
- `current_target`: The element in the DOM that is the current target for
output.
- `document`: The standard `document` object, proxied in workers.
- `window`: The standard `window` object, proxied in workers.
- `js_import`: Function to dynamically import JS modules.
- `js_modules`: Object containing JS modules available to Python.
- `sync`: Utility for synchronizing between worker and main thread.
- `display`: Function to render Python objects in the web page.
- `HTML`: Helper class to create HTML content for display.
- `fetch`: Function to perform HTTP requests.
- `Storage`: Class representing browser storage (local/session).
- `storage`: Object to interact with browser's local storage.
- `WebSocket`: Class to create and manage WebSocket connections.
- `when`: Function to register event handlers on DOM elements.
- `Event`: Class representing user defined or DOM events.
- `py_import`: Function to lazily import Pyodide related Python modules.

If running in the main thread, the following additional names are available:

- `create_named_worker`: Function to create a named Web Worker.
- `workers`: Object to manage and interact with existing Web Workers.

All of these names are defined in the various submodules of `pyscript` and
are imported and re-exported here for convenience. Please refer to the
respective submodule documentation for more details on each component.


!!! Note
Some notes about the naming conventions and the relationship between
various similar-but-different names found within this code base.

```python
import pyscript
```

The `pyscript` package contains the main user-facing API offered by
PyScript. All the names which are supposed be used by end users should
be made available in `pyscript/__init__.py` (i.e., this source file).

```python
import _pyscript
```

The `_pyscript` module is an internal API implemented in JS. **End users
should not use it directly**. For its implementation, grep for
`interpreter.registerJsModule("_pyscript",...)` in `core.js`.

```python
import js
```

The `js` object is
[the JS `globalThis`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/globalThis),
as exported by Pyodide and/or Micropython's foreign function interface
(FFI). As such, it contains different things in the main thread or in a
worker, as defined by web standards.

```python
import pyscript.context
```

The `context` submodule abstracts away some of the differences between
the main thread and a worker. Its most important features are made
available in the root `pyscript` namespace. All other functionality is
mostly for internal PyScript use or advanced users. In particular, it
defines `window` and `document` in such a way that these names work in
both cases: in the main thread, they are the "real" objects, in a worker
they are proxies which work thanks to
[coincident](https://github.com/WebReflection/coincident).

```python
from pyscript import window, document
```

These are just the `window` and `document` objects as defined by
`pyscript.context`. This is the blessed way to access them from `pyscript`,
as it works transparently in both the main thread and worker cases.
"""

from polyscript import lazy_py_modules as py_import
from pyscript.context import (
RUNNING_IN_WORKER,
PyWorker,
config,
current_target,
document,
js_import,
js_modules,
sync,
window,
)
from pyscript.display import HTML, display
from pyscript.fetch import fetch
from pyscript.storage import Storage, storage
from pyscript.websocket import WebSocket
from pyscript.events import when, Event

if not RUNNING_IN_WORKER:
from pyscript.workers import create_named_worker, workers
Loading