Introduction to Python SDK: Interact with MindsDB Directly from Python

Cover Image for Introduction to Python SDK: Interact with MindsDB Directly from Python

MindsDB developed the Python SDK that enables users to interact with MindsDB directly from Python code. You can leverage the power of AI in your applications without the need for ETL pipelines or model-serving architecture.

In this blog post, we’ll show you how to use our Python SDK to create and train models directly in Python.

How to Use Python SDK

To use MindsDB’s Python SDK, you need to install it via pip, as below:

pip install mindsdb_sdk

You can connect to the MindsDB server from Python using HTTP API:

server_local = mindsdb_sdk.connect('http://127.0.0.1:47334')
server_cloud = mindsdb_sdk.connect(login='a@b.com', password='pass123')

If you installed MindsDB locally via pip or Docker, use the connect method with one argument being your IP address and port. And if you use the MindsDB Cloud account, provide your login and password to connect.

Working with MindsDB Objects

Python SDK provides all methods necessary to interact with databases, tables, projects, models, views, and jobs. These include listing available objects, fetching objects with the get method, creating new objects, making predictions, and more.

Once you store the connection to MindsDB in the server variable, you can use it to fetch one of the connected data sources, like this:

database = server.get_database('example_db')

Now let’s get a table stored in this data source:

table = database.get_table('demo_data.home_rentals')

To get a project, we use again the server variable:

project = server.get_project(‘mindsdb’)

In MindsDB, projects are a natural way to keep artifacts, such as models or views, separate according to what predictive task they solve. You can learn more about MindsDB projects here.

The models, views, and jobs reside within projects. So to list or fetch them, you call the methods on the project variable.

Let’s save one of the models available in the mindsdb project, so we can make predictions:

model = project.get_model(‘home_rentals_model’)

Here is how to make predictions:

predictions = model.predict(table)

The predict method is called on the model variable. The table variable stores the input data used to make predictions.

Visit our documentation to see more examples of how to use MindsDB’s Python SDK.

What’s Next

If you are already familiar with MinsdDB, we encourage you to try interacting with MindsDB via the Python SDK. It enables you to automate the process of training models and making predictions by running Python code.

And if you are new to MindsDB, go ahead and create a demo account to explore MindsDB Cloud. Join our Slack community to ask questions and share feedback.