### Install PyWry with Pip
Source: https://github.com/openbb-finance/pywry/blob/main/README.md
This command installs the PyWry library from PyPI using pip. It's the standard method for adding PyWry to your Python project.
```bash
pip install pywry
```
--------------------------------
### Develop PyWry from Source
Source: https://github.com/openbb-finance/pywry/blob/main/README.md
These steps outline the process for installing PyWry from its source code for development purposes. It includes cloning the repository, installing Rust, setting up a virtual environment, and building/installing the package using maturin.
```bash
git clone https://github.com/OpenBB-finance/pywry.git
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
python -m venv venv
source venv/bin/activate # Unix
venv\Scripts\activate # Windows
pip install .[dev]
maturin build
pip install [file path from above] --force-reinstall
```
--------------------------------
### Install WebKitGTK on Debian/Ubuntu
Source: https://github.com/openbb-finance/pywry/blob/main/README.md
This command installs the WebKitGTK development files on Debian or Ubuntu systems. These are necessary dependencies for PyWry, as the underlying windowing system (Tao) and WebView component rely on GTK and WebKitGTK respectively.
```bash
sudo apt install libwebkit2gtk-4.0-dev
```
--------------------------------
### Basic PyWry Webview Usage
Source: https://github.com/openbb-finance/pywry/blob/main/README.md
This Python script demonstrates the basic usage of PyWry. It initializes the PyWry handler, sends simple HTML content to display, starts the backend, and runs an asyncio event loop to keep the application alive. It includes error handling for KeyboardInterrupt.
```python
import asyncio
import sys
from pywry import PyWry
async def main_loop():
while True:
await asyncio.sleep(1)
if __name__ == "__main__":
try:
handler = PyWry()
handler.send_html("
Welcome to PyWry!
")
handler.start()
# PyWry creates a new thread for the backend,
# so we need to run the main loop in the main thread.
# otherwise, the program will exit immediately.
handler.loop.run_until_complete(main_loop())
except KeyboardInterrupt:
print("Keyboard interrupt detected. Exiting...")
sys.exit(0)
```
--------------------------------
### Install WebKitGTK on Fedora/CentOS/AlmaLinux
Source: https://github.com/openbb-finance/pywry/blob/main/README.md
This command installs the necessary GTK3 and WebKitGTK development packages on Fedora, CentOS, or AlmaLinux. These are prerequisites for PyWry to operate correctly on these Linux distributions, facilitating window management and WebView rendering.
```bash
sudo dnf install gtk3-devel webkit2gtk3-devel
```
--------------------------------
### Install WebKitGTK on Arch Linux
Source: https://github.com/openbb-finance/pywry/blob/main/README.md
This command installs the WebKitGTK library on Arch Linux or Manjaro systems. This is a dependency required for PyWry to function correctly on these Linux distributions, as Tao uses GTK for window creation and WebKitGTK for the WebView.
```bash
sudo pacman -S webkit2gtk
```
--------------------------------
### JavaScript: Dynamic Form Generation and Data Submission with pywry
Source: https://github.com/openbb-finance/pywry/blob/main/examples/js_interaction.html
This JavaScript code dynamically generates HTML form elements (labels and inputs) based on provided JSON data. It also includes functionality to capture input values, validate required fields, and send the data to the backend using `window.pywry.result()`. It handles both string and object JSON inputs and includes event listeners for form submission via button click or pressing the Enter key.
```javascript
const exampleDiv = document.getElementById('pywry_example');
function toTitleCase(str) {
return str.replace(/_/g, ' ').replace(/\w\S*/g, (txt) => {
return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();
});
}
function sendData() {
const data = {};
let missing = false;
const inputs = exampleDiv.getElementsByTagName('input');
const labels = exampleDiv.getElementsByTagName('label');
for (let i = 0; i < inputs.length; i++) {
const input = inputs[i];
if (!input.value && input.required) {
labels[i].style.color = 'red';
missing = true;
}
data[input.name] = input.value;
}
if (!missing) {
window.pywry.result(JSON.stringify(data));
}
}
if (window.json_data) {
let json_data = window.json_data;
if (typeof json_data === 'string') {
json_data = JSON.parse(json_data);
}
let pywry_example = exampleDiv.innerHTML;
if (json_data.required) {
json_data.required.forEach((parm) => {
pywry_example += ``;
pywry_example += ` `;
});
}
if (json_data.optional) {
json_data.optional.forEach((parm) => {
pywry_example += ``;
pywry_example += ` `;
});
}
pywry_example += ``;
exampleDiv.innerHTML = pywry_example;
document.addEventListener('keydown', (event) => {
if (event.keyCode === 13) {
sendData();
}
});
}
```
--------------------------------
### PyWry: Passing JSON Data to Frontend
Source: https://github.com/openbb-finance/pywry/blob/main/README.md
This Python snippet shows how to pass JSON data to the frontend using PyWry's `send_html` method. It specifies an HTML file and provides a dictionary for `json_data`, which will be accessible in the JavaScript frontend.
```python
from pathlib import Path
# code from above ...
# change send_html line to:
handler.send_html(
html=Path(__file__).parent / "index.html", json_data={"name": "PyWry"}
)
```
--------------------------------
### HTML Frontend: Accessing JSON Data
Source: https://github.com/openbb-finance/pywry/blob/main/README.md
This HTML code demonstrates how the frontend can access JSON data passed from the Python backend via PyWry. The `window.onload` function checks if `window.json_data` is a string (and parses it if necessary) and then uses the data to update an HTML element.
```html
Hello, !
```
--------------------------------
### Plotly Chart Rendering with PyWry (JavaScript)
Source: https://github.com/openbb-finance/pywry/blob/main/examples/plotly_figure/plotly.html
This JavaScript code snippet is designed to be embedded in a web page to render Plotly charts received from a Python backend (PyWry). It polls for JSON data, parses it, configures Plotly for responsive behavior, and updates the plot. Dependencies include Plotly.js.
```javascript
window.PlotlyConfig = { MathJaxConfig: "local" };
var interval = setInterval(function () {
// When sending a figure json to PyWry, it is stored in the window object
// under the name "json_data". If it exists, we can plot it.
if (window.json_data) {
clearInterval(interval);
// If the json_data is a string, we need to parse it
if (typeof window.json_data === "string") {
window.json_data = JSON.parse(window.json_data);
}
// We need to set the height and width from the layout
// to undefined for the plot to be responsive
window.json_data.layout.height = undefined;
window.json_data.layout.width = undefined;
const CONFIG = {
responsive: true,
displaylogo: false,
scrollZoom: true,
};
Plotly.setPlotConfig(CONFIG);
Plotly.newPlot("myDiv", window.json_data);
}
}, 20);
```
=== COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.