Let's use a simple example to demonstrate how to use Leviathan Platform SDK through Python.
First, create a workspace folder hello_world.
$ mkdir hello-world
Go to the folder and install the base Leviathan SDK:
$ cd hello-world
$ pdm init # Initializing
$ pdm add levrt
There will be a prompt to select the python interpreter version and other options. Don't worry, just press the enter key all the way down.
When the prompt changes are written to pyproject.toml
appears on the screen, that means the initialization was successful.
Then start installing Leviathan SDK levrt
:
$ pdm add levrt
When the All Complete
prompt appears, it means installation was successful.
Now the initial environment configuration has completed, and the final project structure will be:
hello-world/
├── pdm.lock
├── .pdm.toml # Hidden file, generally not displayed
├── pyproject.toml
└── __package__/
Create a new main.py
file under the workspace (hello-world) and enter:
import levrt
from levrt import Cr
def hello():
return Cr("alpine:latest",cmd=["echo","hello,world"])
async def main():
await hello()
if __name__ == "__main__":
levrt.run(main ())
This step does not explain the specific meaning of the code for the time being. Let's firstly complete the remain steps.
Open the terminal, enter the workspace folder to execute main.py
.
$ cd hello-world
$ python3.10 main.py
If pdm is installed by the brew command under mac, you need to execute the command:
$ cd hello-world
$ pdm run python main.py
If no exception is reported and there is no response after the command is executed, it means the operation is successful.
All the tool invocation runs in docker. If there is no output after the previous code is executed, the final running result needs to be viewed in docker.
Enter the docker command:
$ docker ps -a
The content shown in the figure below will appear. The red box is the container for our running code:
Enter the command to print the log and to view the final result:
# 6d52 is the abbreviation of the ID of the docker container in the above figure.
$ sudo docker logs -f 6d52
hello,world
Successfully print out "HelloWorld" log.
Congratulations! You have successfully used the Leviathan SDK once and printed "hello, world" in the container! The long march begins with a single step, I believe you will soon be able to learn more advanced features.
# Import Leviathan SDK - levrt package
import levrt
from levrt import Cr
# Main function method, execute core logic
def hello():
# 执行结果写入docker中
return Cr("alpine:latest", cmd=["echo", "hello,world"])
# Asynchronous method encapsulation, each function method of the user needs to be encapsulated by an asynchronous method
async def main():
await hello()
# Main function, program entry
if __name__ == "__main__":
# Call the SDK execution function
levrt.run(main ())