Doltpy
This shows how to execute basic dolt commands using Dolt's Python libraries.
Note: doltcli and doltpy.cli are the same, but we default to doltcli in examples because Windows and Conda users have additional dependency hurdles with doltpy.
Source code:
For long-form content refer to the Doltpy Tutorials.

Setup

Install

1
sudo bash -c 'curl -L https://github.com/dolthub/dolt/releases/latest/download/install.sh | sudo bash'
2
pip3 install --user doltcli doltpy
Copied!

Create Repo

1
>>> import doltcli as dolt
2
>>> db = dolt.Dolt.init("new-database-folder")
Copied!

Clone Repo

1
>>> import doltcli as dolt
2
>>> db = dolt.Dolt.clone("max-hoffman/qm9")
Copied!

Connect to Existing Repo Folder

1
>>> import doltcli as dolt
2
>>> db = dolt.Dolt("existing-database-folder")
Copied!

Version Control Commands

Checkout New Branch

1
>>> db.checkout(branch="new_branch", checkout_branch=True)
2
>>> db.sql("select DOLT_CHECKOUT('-b', 'new_branch')")
Copied!

Checkout Existing Branch

1
>>> db.checkout(branch="existing_branch")
2
>>> db.sql("select DOLT_CHECKOUT('existing_branch')")
Copied!

Add a Table

1
>>> db.add("table-to-add")
2
>>> db.add(".")
3
Status(is_clean=False, modified_tables={}, added_tables={'test': True})
4
>>> db.sql("select DOLT_ADD('.')")
Copied!

Commit a Change

1
>>> db.commit(message="commit message")
2
>>> db.sql("select DOLT_COMMIT('-am', 'commit message')")
Copied!

Reset

1
>>> db.checkout(".")
2
>>> db.reset()
3
>>> db.reset(soft=True)
4
>>> db.reset(".")
5
>>> db.reset("a-table")
6
>>> db.reset(hard=True)
Copied!

Log

1
>>> db.log(1)
2
OrderedDict([('1uhml9dartkb8lndr643blsclgno9kbt', Commit(ref='1uhml9dartkb8lndr643blsclgno9kbt', timestamp='2021-06-24 13:42:37.149 -0700 PDT', author='Max Hoffman', email='[email protected]', message='Initialize data repository', parents='', merge=False))])
3
>>> db.sql("select * from dolt_log", result_format="csv")
4
[OrderedDict([('commit_hash', '1uhml9dartkb8lndr643blsclgno9kbt'), ('committer', 'Max Hoffman'), ('email', '[email protected]'), ('date', '2021-06-24 13:42:37.149 -0700 PDT'), ('message', 'Initialize data repository')])]
Copied!

Merge

1
>>> db.merge(branch="master")
Copied!

Remote

1
>>> db.remote(name="origin", url="max-hoffman/qm9", add=True)
2
>>> db.remote(name="origin", url="aws://test-bucket", add=True)
3
>>> db.remote(name="origin", url="gs://test-bucket", add=True)
Copied!

Push

1
>>> db.push(remote="origin", refspec="master")
Copied!

Pull

1
>>> db.pull(remote="origin")
Copied!

Reading/Writing

File

1
>>> from doltcli import write_file
2
>>> write_file(
3
dolt=db,
4
table="test",
5
file_handle=open("test.csv", "r"), # io.Text
6
import_mode="create",
7
)
8
>>> write_file(
9
dolt=db,
10
table="test",
11
file="test.csv", # str or pathlib.Path
12
import_mode="create",
13
)
Copied!

Dictionary

1
>>> from doltcli import write_rows
2
>>> write_rows(
3
dolt=db,
4
table="test",
5
rows=[{"name": "User 1"}],
6
import_mode="create",
7
)
Copied!
1
>>> from doltcli import read_rows
2
>>> read_rows(dolt=db, table="test")
Copied!

Array

1
>>> from doltcli import write_columns
2
>>> write_columns(
3
dolt=db,
4
table="test",
5
columns={"name": ["User 1"]},
6
import_mode="create",
7
)
Copied!
1
>>> from doltcli import read_columns
2
>>> read_columns(dolt=db, table="test")
Copied!

Pandas

1
>>> from doltpy.cli.write import write_pandas
2
>>> df = pd.DataFrame({'name' : ['User 1', 'User 2', 'User 3']})
3
>>> write_pandas(
4
dolt=db,
5
table="test",
6
df=df,
7
import_mode="create",
8
)
Copied!
1
>>> from doltpy.cli.read import read_pandas
2
>>> df = read_pandas(
3
dolt=db,
4
table="test",
5
)
Copied!
Last modified 2mo ago