| < 4.1 SI Overview | Table of Contents | 4.3 Scene and Object > |
This part of the functions is about controlling some basic features, some provided by the browser and others are derived from Lua debugging.
The browser provides JavaScript running environment, storage and network communication features.
Interface to the underlying JavaScript engine. This function will await eval() the given JavaScript and return the result as a string.
print(os.execute("Math.floor(Math.random())")) --use js Math lib and print out the result
Dynamically executing javascript can greatly expand the functionality of MicroCity Web. Please note that the Lua engine runs in a web worker, so accessing the DOM requires executing the auxiliary function RemoteCall. Here is a simple example:
os.execute("RemoteCall('alert', 'hello!')") --Remotely call (accessing DOM) the js alert function
A more complex example is to create an application using Python’s fastapi and call it in MicroCity Web:
1. First install fastapi: pip install fastapi uvicorn
2. Then create main.py:
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
app = FastAPI()
app.add_middleware(
CORSMiddleware,
allow_origins=['\*'],
allow_credentials=True,
allow_methods=['\*'],
allow_headers=['\*'],
)
@app.get("/msg")
async def read_msg():
return {"msg": "Hello from FastAPI"}
3. Execute main.py: uvicorn main:app --reload
4. Finally run the following script in MicroCity Web:
print(os.execute("(async () => (await (await fetch('http://127.0.0.1:8000/msg')).json()).msg)()"))
or
print(os.execute("fetch('http://127.0.0.1:8000/msg').then(res => res.json()).then(data => data.msg)"))
Sleep for ms milliseconds.
Suspend the execution if Pause is clicked or return false if Stop
is clicked.
It is useful for controlling a long run script. User can pause it and run some commands in the console or stop it.
Access the file from url or prompt user choosing local files to upload to the virtual disk.
This function is used to facilitate the script accessing external files. User can upload files first, then start the program.
Access the file from fpath or prompt user choosing files to download from the virtual disk.
These functions can trigger remote REST API for Chat Completions and Embeddings. The API can be customized.
Given a system message (prompt) and a user message comprising a conversation, the model will return a response.
Creates an embedding vector (an array with 1024 floats) representing the input text.
The Debug library retains the original functions of Lua and made modifications. Users can execute them in scripts or on the console.
Enter debug mode (no parameters or b is true) or exit debug mode (b is false).
Pause the execution in debug mode.
Continue the execution in debug mode.
Step over the execution in debug mode.
Step in the execution in debug mode.
Step out the execution in debug mode.
Add a variable with the name (string) to the watchlist or clear the watchlist (without name).
| < 4.1 SI Overview | Table of Contents | 4.3 Scene and Object > |