|
62 | 62 |
|
63 | 63 | # surrealdb.py
|
64 | 64 |
|
65 |
| -The official SurrealDB library for Python. |
| 65 | +The official SurrealDB SDK for Python. |
66 | 66 |
|
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) |
68 | 68 |
|
69 | 69 | ## Getting Started
|
70 | 70 | Below is a quick guide on how to get started with SurrealDB in Python.
|
71 | 71 |
|
72 | 72 | ### 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/) |
96 | 75 |
|
97 |
| -### Installing the Python Library |
| 76 | +After we have everything up and running, we can install the Python SDK. |
98 | 77 |
|
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 |
103 | 79 |
|
104 | 80 | ```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 |
154 | 82 | ```
|
155 | 83 |
|
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 |
158 | 85 |
|
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: |
162 | 87 |
|
163 | 88 | ```python
|
164 | 89 | from surrealdb import SurrealDB
|
165 | 90 |
|
166 |
| -connection = SurrealDB("ws://localhost:8000/database/namespace") |
| 91 | +db = SurrealDB("ws://localhost:8000/database/namespace") |
167 | 92 | ```
|
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`. |
170 | 97 | We need a database and namespace to connect to SurrealDB.
|
171 | 98 |
|
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: |
173 | 100 |
|
174 | 101 | ```python
|
175 |
| -connection.signin({ |
| 102 | +db.signin({ |
176 | 103 | "username": "root",
|
177 | 104 | "password": "root",
|
178 | 105 | })
|
179 | 106 | ```
|
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. |
182 | 108 |
|
183 | 109 | ```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;") |
187 | 113 | print(outcome)
|
188 | 114 | ```
|
189 | 115 |
|
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 |
206 | 117 |
|
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. |
217 | 121 |
|
218 | 122 | ```python
|
219 | 123 | import asyncio
|
220 | 124 | from surrealdb import AsyncSurrealDB
|
221 | 125 |
|
222 | 126 |
|
223 | 127 | 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({ |
227 | 131 | "username": "root",
|
228 | 132 | "password": "root",
|
229 | 133 | })
|
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;") |
233 | 137 | print(outcome)
|
234 | 138 |
|
235 | 139 |
|
236 | 140 | # Run the main function
|
237 | 141 | asyncio.run(main())
|
238 | 142 | ```
|
| 143 | + |
| 144 | +### Using Jupyter Notebooks |
| 145 | + |
| 146 | +The Python SDK currently only supports the `AsyncSurrealDB` methods. |
0 commit comments