Skip to content

Commit 5bbdc74

Browse files
committed
update readme
1 parent 11409ad commit 5bbdc74

File tree

1 file changed

+34
-126
lines changed

1 file changed

+34
-126
lines changed

Diff for: README.md

+34-126
Original file line numberDiff line numberDiff line change
@@ -62,177 +62,85 @@
6262

6363
# surrealdb.py
6464

65-
The official SurrealDB library for Python.
65+
The official SurrealDB SDK for Python.
6666

67-
[See the documentation here](https://surrealdb.com/docs/integration/libraries/python)
67+
[See the documentation here](https://surrealdb.com/docs/surrealdb/integration/sdks/python)
6868

6969
## Getting Started
7070
Below is a quick guide on how to get started with SurrealDB in Python.
7171

7272
### Running SurrealDB
73-
Before we can do anything, we need to download SurrealDB and start the server. The easiest, cleanest way to do this
74-
is with [Docker](https://www.docker.com/) abd [docker-compose](https://docs.docker.com/compose/) with the following
75-
docker-compose file:
76-
77-
```yaml
78-
version: '3'
79-
services:
80-
surrealdb:
81-
image: surrealdb/surrealdb
82-
command: start
83-
environment:
84-
- SURREAL_USER=root
85-
- SURREAL_PASS=root
86-
- SURREAL_LOG=trace
87-
ports:
88-
- 8000:8000
89-
```
90-
Here we are pulling the offical SurrealDB image from Docker Hub and starting it with the `start` command. We are also
91-
setting the environment variables `SURREAL_USER` and `SURREAL_PASS` to `root` and `SURREAL_LOG` to `trace`. This will
92-
allow us to connect to the database with the username `root` and password `root` and will set the log level to `trace`.
93-
Finally, we are exposing port `8000` so we can connect to the database.
94-
95-
Now that we have everything up and running, we can move onto installing the Python library.
73+
Before we can do anything, we need to download SurrealDB and start the server.
74+
[See how to do that here](https://surrealdb.com/docs/surrealdb/installation/)
9675

97-
### Installing the Python Library
76+
After we have everything up and running, we can install the Python SDK.
9877

99-
Right now the library is in beta, so you will need to install it from GitHub with Rust installed on your machine
100-
as Rust will be needed to compile part of the library. Installation for Rust can be found
101-
[here](https://www.rust-lang.org/tools/install). Once Rust is installed, you can install this library with the
102-
following command:
78+
### Installing the Python SDK
10379

10480
```bash
105-
pip install git+https://github.com/surrealdb/surrealdb.py@rust-no-runtime
106-
```
107-
108-
Installation can take a while as it needs to compile the Rust code. If you want to use the python client in a Docker
109-
build in production you can use a two layered build which will use Rust to compile the library and then copy the
110-
compiled library into a new python image so your production image doesn't need to have Rust installed. Below is an
111-
example of a Dockerfile that will do this for a Flask application:
112-
113-
```dockerfile
114-
FROM rust:latest as builder
115-
116-
RUN apt-get update
117-
RUN apt install python3.9 -y
118-
RUN apt-get install -y python3-dev -y
119-
RUN apt-get install -y python3-pip -y
120-
RUN pip install --upgrade pip setuptools wheel
121-
122-
RUN apt-get install libclang-dev -y
123-
124-
# Set the working directory to /app
125-
WORKDIR /app
126-
127-
# Copy the current directory contents into the container at /app
128-
ADD . /app
129-
130-
# install the python library locally
131-
RUN pip install ./surreal.py/
132-
133-
# or install the python library from github
134-
RUN pip install git+https://github.com/surrealdb/surrealdb.py@rust-no-runtime
135-
136-
# server build
137-
FROM python:3.9
138-
139-
RUN apt-get update \
140-
&& apt-get install -y python3-dev python3-pip \
141-
&& pip install --upgrade pip setuptools wheel \
142-
&& pip install flask gunicorn
143-
144-
WORKDIR /app
145-
146-
# copy the built python packages from the previous stage to the new image
147-
COPY --from=builder /usr/local/lib/python3.9/dist-packages /usr/local/lib/python3.9/site-packages
148-
149-
# copy the python server app code to the new image
150-
COPY --from=builder /app /app
151-
152-
# Run the python server using gunicorn
153-
CMD ["gunicorn", "-w", "4", "-b", "0.0.0.0:5002", "--timeout", "900", "main:app"]
81+
pip install surrealdb
15482
```
15583

156-
You can change the python version and the `CMD` could change depending on your application. Now that we have our
157-
python application installing and running with SurrealDB, we can move onto using the python library.
84+
### Using the (synchronous) Python methods
15885

159-
### Using the blocking Python Library
160-
161-
We can then import the library and create the connection with the following code:
86+
Import the SDK and create the database connection:
16287

16388
```python
16489
from surrealdb import SurrealDB
16590

166-
connection = SurrealDB("ws://localhost:8000/database/namespace")
91+
db = SurrealDB("ws://localhost:8000/database/namespace")
16792
```
168-
Here, we can see that we defined the connection protocol as websocket using `ws://`. We then defined the host as
169-
`localhost` and the port as `8000`. Finally, we defined the database and namespace as `database` and `namespace`.
93+
94+
Here, we can see that we defined the connection protocol as WebSocket using `ws://`. We then defined the host as `localhost` and the port as `8000`.
95+
96+
Finally, we defined the database and namespace as `database` and `namespace`.
17097
We need a database and namespace to connect to SurrealDB.
17198

172-
Now that we have our connection we need to signin using with the following code:
99+
Now that we have our connection we need to signin:
173100

174101
```python
175-
connection.signin({
102+
db.signin({
176103
"username": "root",
177104
"password": "root",
178105
})
179106
```
180-
For our getting started example we are now going to run some simple raw SurrealQL queries to create some users and
181-
then select them. We can then print the outcome of the query with the following code:
107+
We can now run our queries to create some users, select them and print the outcome.
182108

183109
```python
184-
connection.query("CREATE user:tobie SET name = 'Tobie';")
185-
connection.query("CREATE user:jaime SET name = 'Jaime';")
186-
outcome = connection.query("SELECT * FROM user;")
110+
db.query("CREATE user:tobie SET name = 'Tobie';")
111+
db.query("CREATE user:jaime SET name = 'Jaime';")
112+
outcome = db.query("SELECT * FROM user;")
187113
print(outcome)
188114
```
189115

190-
This will give you the following JSON output:
191-
192-
```json
193-
[
194-
{
195-
"id": "user:jaime",
196-
"name": "Jaime"
197-
},
198-
{
199-
"id": "user:tobie",
200-
"name": "Tobie"
201-
}
202-
]
203-
```
204-
205-
### Using the async Python Library
116+
### Using the async Python methods
206117

207-
We can then import the library and create the connection with the following code:
208-
209-
```python
210-
from surrealdb import AsyncSurrealDB
211-
212-
connection = AsyncSurrealDB("ws://localhost:8000/database/namespace")
213-
await connection.connect()
214-
```
215-
216-
Essentially the interface is exactly the same however, the functions are async. Below is a way to run the async code:
118+
The async methods work in the same way, with two main differences:
119+
- Inclusion of `async def / await`.
120+
- You need to call the connect method before signing in.
217121

218122
```python
219123
import asyncio
220124
from surrealdb import AsyncSurrealDB
221125

222126

223127
async def main():
224-
connection = AsyncSurrealDB("ws://localhost:8000/database/namespace")
225-
await connection.connect()
226-
await connection.signin({
128+
db = AsyncSurrealDB("ws://localhost:8000/database/namespace")
129+
await db.connect()
130+
await db.signin({
227131
"username": "root",
228132
"password": "root",
229133
})
230-
await connection.query("CREATE user:tobie SET name = 'Tobie';")
231-
await connection.query("CREATE user:jaime SET name = 'Jaime';")
232-
outcome = await connection.query("SELECT * FROM user;")
134+
await db.query("CREATE user:tobie SET name = 'Tobie';")
135+
await db.query("CREATE user:jaime SET name = 'Jaime';")
136+
outcome = await db.query("SELECT * FROM user;")
233137
print(outcome)
234138

235139

236140
# Run the main function
237141
asyncio.run(main())
238142
```
143+
144+
### Using Jupyter Notebooks
145+
146+
The Python SDK currently only supports the `AsyncSurrealDB` methods.

0 commit comments

Comments
 (0)