Test Code Remotely with Ratio1 – Build Your Own Sandbox in Minutes

Education

Build Your Own Sandbox in Minutes
Build Your Own Sandbox in Minutes

**Important Update:** This blogpost predates version 2.5 of the Ratio1 Python SDK (previously known as the Naeural Edge Protocol SDK), so some methods, calls, and processes may have changed. For the most accurate information, please consult our latest posts, videos, and tutorials. Currently, the SDK is listed on PyPI as `naeural_client`, but going forward, we plan to rename it to simply `ratio1`.

Introduction

Imagine that you can test your code remotely in a secure, decentralized environment. Well, that is not only possible but also incredibly easy with the Ratio1 Protocol. Today, I’m going to show you how to use the web app at sandbox.ratio1.ai, where you can play with code using our ready-made templates or create something from scratch. I’ll also guide you on how to build your own sandbox in just a few steps.

To put that into perspective, here is a screenshot of the web app interface:

Preview

Getting Started with sandbox.ratio1.ai

The web app offers users a variety of ready-to-use code templates, making it simple to get started. You can either modify these templates to suit your needs or write your own code from scratch.

Code Example: Fibonacci Sequence Prediction

Here’s an example of a template that calculates the Fibonacci sequence and predicts the next elements:

# First 9 Fibonacci elements
x = [1, 1, 2, 3, 5, 8, 13, 21, 34]

# We predict the next 7 elements using the basic_ts_fit_predict method
yh = plugin.basic_ts_fit_predict(data=x, steps=7)
print(yh)

# Actual next 7 values in the Fibonacci sequence
y = [55, 89, 144, 233, 377, 610, 987]

# Compute the mean absolute error of the prediction
yh_np = plugin.np.array(yh)
y_np = plugin.np.array(y)
result = plugin.np.abs(y_np - yh_np).mean()
print(result)

With just a few lines of code, you can run a predictive analysis and even calculate the error of the prediction.

Safety and Security: What Happens if You Run Harmful Code?

For security reasons some operations are not allowed. For example code which attempts to delete files from the processing node will be rejected as will specific imports such as os. To offer some degree of flexibility, though we have made accessible a range of utility functions via our API – as you saw in the Fibonacci example above. Here is an example of a script which would not pass:

import os
for root, dirs, files in os.walk():
    for file in files:
        os.remove(os.path.join(root, file))

# Alternative blocked method using plugin.os
for root, dirs, files in plugin.os.walk():
    for file in files:
        plugin.os.remove(plugin.os.path.join(root, file))

The system is designed to block such unsafe operations while still providing users with powerful tools to experiment within the sandbox.

Build Your Own Code Sandbox in Minutes

Let’s now walk through how you can quickly deploy your own code sandbox using the Ratio1 Python library. No need to clone a GitHub repository - just follow the simple steps below.

Step-by-Step Guide to Deploying Your Own Sandbox

1. Install the Ratio1 Python Library

   pip install naeural_client --upgrade

2. Connect to the network

Use the Ratio1 Python library to connect to a node and deploy your code execution and interface endpoints:

   from naeural_client import Session
   
   session = Session()

   node = "0xai_AwTD0PEqh_dmW3vH_h8N6CyNwLOh4ql2khy7-yP4Ltp-"
   session.wait_for_node(node)

Note: Always make sure your SDK is paired with your node

After updating your SDK via

pip install –upgrade naeura_client

Just run a simple configuration checking using

r1ctl config show

And you will receive something like

The you just copy the 0xai internal address and give it to your node via the container add_allowed command:

docker exec r1node add_allowed 0xai_xxxxxxxxxxxxxxxxxxxxxxxxxx

Where certainly the 0xai_xxxxxxxxxxxxxxxxxxxxxxxxxx is the address copied from your r1ctl config show client command.  As we result you should obtain something similar with this output below where you can clearly see in the allowed addresses whitelist your client address. If this command fails then it means your node is not running or it is running under other docker container name than r1node (only if you make custom special installs)

Note: running the docker exec r1node add_allowed or the docker exec r1node get_node_info commands will show a list of several (quite a few actually) nodes that are allowed on your node by default - these are the oracles that supervise your node and make sure you get your Proof-of-Availability rewards as well as your Proof-of-AI rewards for the specific jobs you receive.

3. Create your Custom WebApp

After connecting to the node we need to define a WebApp instance:

    pipeline, instance = session.create_web_app(
        node=node,
        name="naeural_code_sandbox_copycat_app",
        signature=CustomWebApp01,

        ngrok_edge_label="edghts_2jSQ4nm5TuzGgHh8I0wlfDz3Vr0",  # your ngrok URL
        use_ngrok=True,
    )

The code above will create a WebApp instance that will be accessible through an Ngrok edge at https://naeural-001.ngrok.app/docs .

4. Define an endpoint

We have the domain, let’s create an endpoint.

   def remote_execute(plugin: CustomPluginTemplate, code: str):
    result = plugin.execute_remote_code(code=code)
    return {
        "Congratulations": "You have successfully executed the code!",
        **result,
    }

This method will receive some code and will attempt to execute it and return the result. We can attach it to the domain created before as follows:

    instance.add_new_endpoint(remote_execute, method="post")

After this it will be accessible at https://naeural-001.ngrok.app/remote_execute .

5. Create our interface

Up until now we successfully wrote a WebAPI on which one can execute code. Now let’s attach an interface to it.

    instance.add_new_html_endpoint(
        html_path=<path to your HTML file>

And just like that, you have got an interface where users can write code.

6. Finally deploy

All is left now is just one line of code to start the magic:

    pipeline.deploy()

Full Example Code

Below is the complete code to deploy your custom sandbox web app:

from naeural_client import CustomPluginTemplate, Session
from naeural_client.default.instance import CustomWebApp01

# This tutorial can be run only on the local edge node
# because it uses ngrok to expose the FastAPI server,
# and this requires a ngrok auth token

# See https://naeural-001.ngrok.app/docs

def remote_execute(plugin: CustomPluginTemplate, code: str):
    result = plugin.execute_remote_code(code=code)
    return {
        "Congratulations": "You have successfully executed the code!",
        **result,
    }

if __name__ == "__main__":
    session = Session()

    node = "0xai_AwTD0PEqh_dmW3vH_h8N6CyNwLOh4ql2khy7-yP4Ltp-"
    session.wait_for_node(node)

    instance: CustomWebApp01
    pipeline, instance = session.create_web_app(
        node=node,
        name="naeural_code_sandbox_copycat_app",
        signature=CustomWebApp01,

        ngrok_edge_label="edghts_2jSQ4nm5TuzGgHh8I0wlfDz3Vr0",  # your ngrok URL
        use_ngrok=True,
    )

    # POST request on <domain>/remote_execute (with body as JSON with 1 key: code)
    instance.add_new_endpoint(remote_execute, method="post")

    # Add an HTML file to the web app, accessible at <domain>/
    instance.add_new_html_endpoint(
        html_path=<path to your html file>

Conclusion

Testing code remotely was never easier, and with Ratio1, fast and secure, too. Give the templates available in the sandbox.ratio1.ai a try or create your own sandbox in a few lines of code.

Feel free to give that a shot and share with the community! If you have any questions, go ahead and comment or reach out to us. Happy coding!

Andrei Ionut Damian

Andrei Ionut Damian

Dec 13, 2024

The Ultimate AI OS Powered by Blockchain Technology

©Ratio1 2025. All rights reserved.

The Ultimate AI OS Powered by Blockchain Technology

©Ratio1 2025. All rights reserved.

The Ultimate AI OS Powered by Blockchain Technology

©Ratio1 2025. All rights reserved.