-
DHOMBRES Stephanie authoredDHOMBRES Stephanie authored
Nanospace Python Client
The following scripts demonstrate how to connect to the Nanospace Back-end and how to interact with it to create, read, update or delete components and values, in the Neo4J database.
Note: Nanospace Back-end can be installed from Docker images from demo repository.
1. Requirements
A Nanospace Back-end is required, and the following packages :
sudo apt install python3 python3-pip python-is-python3 git
pip install requests
git clone https://gitlab.isae-supaero.fr/nanostar/nanospace/nanospace-clients/nanospace-python-client.git
Note: You can also use a virtual environment.
2. Scripts
This repository has 3 Python scripts:
- nanospace.py : Nanospace API
- auth_test.py : To test Authentication to Nanospace Back-end
- simple_example.py : To read/write/delete values in the database from a Python script
2.1. Authentication Test to Nanospace-Backend (auth_test.py)
To test the connection to the Nanospace Back-end:
You need to update the script according to your url, username, password. An example is given below:
login_to_api('http://localhost:8888/login', 'test', 'test')
Then run:
python auth_test.py
The response shall be:
Login successful!
<Response [200]>
2.2. Authentication Test to Nanospace-Backend (Terminal)
It is possible to request your token using the following command line :
Note: Replace the url,
<yourLogin>
and<yourPassword>
by your real url, login and password) :
curl -s -i \
http://localhost:8888/login \
-H 'Cache-Control: no-cache' \
-H 'Content-Type: application/json' \
-d '{
"username":"<yourLogin>",
"password":"<yourPassword>"
}' | grep -i "Authorization:"
Response :
```bash
Authorization: Bearer --TokenId-- # Token Result
The result is giving you a token to access to your account.
2.2 Simple Example (simple_example.py)
Check the simple_example.py
Python file. Before try to launch it, change the username
, password
and server
address (End the server address with a /
).
from nanospace import Nanospace
nanospace = Nanospace('http://server_address/','username', 'password')
2.2.1 Creation of element
To create component, value and modes from Python, we advice to use the Nanospace Front-End
UI instead.
But it is possible using :
# from nanospace import Nanospace
# nanospace = Nanospace('http://server_address/','username', 'password')
nanospace._create_component(12, 'Orbitography')
nanospace._create_mode(12, 'Recovery')
nanospace._create_string_value(45, 'Earth_mass')
nanospace._create_formula_value(46, 'Earth_standard_gravitational_parameter')
If the id
is missing, check the Nanospace front-end
url or the #id
, that can help you to get one.
Then each one of the create function are returning a Python dictionary where you can find id.
2.2.2 Read a value
Read a string and a formula using Python:
# from nanospace import Nanospace
# nanospace = Nanospace('http://server_address/','username', 'password')
print(nanospace.get_string_value(<id>))
print(nanospace.get_formula_value(<id>))
To use Curl, copy-paste the <your_token>
of the login (from #2.2) and add the <id>
of the value, you want to get:
curl -s GET http://localhost:8888/value/<id> \
-H 'Cache-Control: no-cache' \
-H 'Content-Type: application/json' \
-H 'Authorization: Bearer <your_token>' | grep value
Example:
curl -s GET http://localhost:8888/value/1 \
-H 'Cache-Control: no-cache' \
-H 'Content-Type: application/json' \
-H 'Authorization: Bearer eyJhbGciOiJIUzUxMiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJ0ZXN0IiwiZXhwIjoxNzQ1NTA1NzA2fQ.GS-iGT6PA-DOpxya5dNLjHY2VOlyKFx9UFEz7ygaHXn1EAVJsIbJMaU3XaIZe0n-z5yi9e-5bIbmmivSYe133w' | grep value
Expected response (if the <id>
exists and the token is valid):
{"id":1,"creationDateTime":"2019-05-17T17:13:24.497","lastUpdateDateTime":"2025-04-14T12:32:35.761811921","name":"Earth_mass","text":"Earth_mass","description":"","type":"string_value","responsibles":[],"value":"5.9722*10^(24)"}
2.2.3 Update a value
Update a string :
# from nanospace import Nanospace
# nanospace = Nanospace('http://server_address/','username', 'password')
print(nanospace.get_string_value(66))
nanospace.update_value(66, 'string_example', '9000000')
print(nanospace.get_string_value(66)) # Should print 9000000
Update a formula :
# from nanospace import Nanospace
# nanospace = Nanospace('http://server_address/','username', 'password')
print(nanospace.get_formula_value(77))
nanospace.update_value(77, 'formula_-_example', '9000000')
print(nanospace.get_formula_value(77)) # Should print 9000000
2.2.4 Delete a element
To delete element from Python :
# from nanospace import Nanospace
# nanospace = Nanospace('http://server_address/','username', 'password')
nanospace._delete_component(12)
nanospace._delete_mode(45)
nanospace._delete_value(63)
It is advised to delete element from the UI.
Note : that delete_value is working either if the value is a formula, or a string.