# Download model via \`pystardog\` or HTTP API

**URL:** <https://community.stardog.com/t/download-model-via-pystardog-or-http-api/4450>\
**Category:** Stardog Studio\
**Created:** [June 27, 2023, 10:28pm UTC](https://community.stardog.com/t/download-model-via-pystardog-or-http-api/4450 "2023-06-27T22:28:23Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![hall00ween](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.stardog.com/hall00ween/32/1752_2.png) [@hall00ween](https://community.stardog.com/u/hall00ween)\
**Post date:** [June 27, 2023, 10:28pm UTC](https://community.stardog.com/t/download-model-via-pystardog-or-http-api/4450/1 "2023-06-27T22:28:23Z")

</div>

I am looking to programmatically download the model (specifically what's shown in the 'Text Editor' in Stardog Studio - see image below). Is there a way to do this through `pystardog` or the HTTP API?

 ![image](https://canada1.discourse-cdn.com/flex030/uploads/stardog/original/2X/2/215ed410c1c47309bbd867a2ed745a84296ed048.jpeg)

**(Ultimately, what I am interested in is downloading the Turtle file generated by clicking the button in the photo above.)**

Using the ["Generate model" GET endpoint from the HTTP API](https://stardog-union.github.io/http-docs/#tag/DB-Admin/operation/generateModel) gets me close but it doesn't include the SHACL constraints at the bottom of the text editor.

 ![image](https://canada1.discourse-cdn.com/flex030/uploads/stardog/original/2X/3/36a4300c5f3fbaf0075bdd31c28ef6f7550a3ef6.png)

---

<div class="post-metadata">

**Author:** ![hall00ween](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.stardog.com/hall00ween/32/1752_2.png) [@hall00ween](https://community.stardog.com/u/hall00ween)\
**Post date:** [July 19, 2023, 5:21pm UTC](https://community.stardog.com/t/download-model-via-pystardog-or-http-api/4450/2 "2023-07-19T17:21:13Z")

</div>

I scoured the API Reference website a few times now. This feature doesn't appear to be available as of Stardog 9.1.0.

[https://stardog-union.github.io/http-docs/](https://stardog-union.github.io/http-docs/)

---

<div class="post-metadata">

**Author:** ![noahgorstein](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.stardog.com/noahgorstein/32/584_2.png) [@noahgorstein](https://community.stardog.com/u/noahgorstein)\
**Post date:** [July 19, 2023, 7:34pm UTC](https://community.stardog.com/t/download-model-via-pystardog-or-http-api/4450/3 "2023-07-19T19:34:06Z")

</div>

Hi Jamie.

I think what you're looking for is to _export_ a named graph contained in a database which is what that "download" button is doing in Studio.

**REST AP** I: [API Reference | ReDoc](https://stardog-union.github.io/http-docs/#tag/DB-Admin/operation/exportDatabase) -

> **Note** : I think what may be missing from this documentation is the `graph_uri` query parameter which will allow you to export just a named graph from the database instead of the entire database.

**pystardog docs** : [Modules — pystardog documentation](https://pystardog.readthedocs.io/en/latest/source/stardog.html#stardog.connection.Connection.export)

Below is example using pystardog that shows how to export a graph/model 2 different ways.

1. with streaming the contents of the model/graph back (avoids reading the graph into memory all at once - useful for large graphs)
2. just getting the contents of the graph back all at once:

```python
import stardog

connection_details = {
    "endpoint": "https://sd-######.stardog.cloud:5820",
    "username": "someuser",
    "password": "somepassword",
    "database": "my_database",
}
model_uri = "urn:example:starwars:model"

# streams an export of the graph/model serialized as Turtle
# a generator that yields bytes making up the graph/model is returned
# avoids reading the graph/model at once into memory for large graph/models
# `chunk_size` parameter available to control the amount of bytes to read per chunk when streaming
model = ""
with stardog.Connection(**connection_details) as conn:
    with conn.export(
        graph_uri=model_uri, stream=True, content_type=stardog.content_types.TURTLE
    ) as stream:
        for chunk in stream:
            model += chunk.decode("utf-8")

print(f"---MODEL (STREAMED)---\n{model}\n\n")

# exports the graph/model serialized as turtle
# send whole response (graph/model) back as as bytes
with stardog.Connection(**connection_details) as conn:
    model = conn.export(graph_uri=model_uri, content_type=stardog.content_types.TURTLE)

print(f"---MODEL---\n{model.decode('utf-8')}")

```

Hope that helps!

Cheers,  
Noah

---

<div class="post-metadata">

**Author:** ![hall00ween](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.stardog.com/hall00ween/32/1752_2.png) [@hall00ween](https://community.stardog.com/u/hall00ween)\
**Post date:** [July 24, 2023, 7:07pm UTC](https://community.stardog.com/t/download-model-via-pystardog-or-http-api/4450/4 "2023-07-24T19:07:15Z")

</div>

Hi Noah,

Thank you. With your help, I was able to export a named graph from a database.

I am attempting to import a named graph to a database, and doing so unsuccessfully. Below is the code I use, though the named graph doesn't populate in Stardog Studio under the database.

```auto
source_model = ''
with stardog.Connection(database=source_database, **conn_details) as conn:
    with conn.export(graph_uri=source_model_uri, stream=True, content_type=stardog.content_types.TURTLE) as stream:
            for chunk in stream:
                source_model += chunk.decode('utf-8')

target_model = stardog.content.Raw(source_model, content_type=stardog.content_types.TURTLE)
with stardog.Connection(database=target_database, **conn_details) as conn:
    conn.begin()
    conn.add(target_model, graph_uri=target_model_uri, server_side=False)
    conn.commit()

```

Is there something I am missing or doing incorrectly?

---

<div class="post-metadata">

**Author:** ![hall00ween](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.stardog.com/hall00ween/32/1752_2.png) [@hall00ween](https://community.stardog.com/u/hall00ween)\
**Post date:** [July 24, 2023, 8:56pm UTC](https://community.stardog.com/t/download-model-via-pystardog-or-http-api/4450/5 "2023-07-24T20:56:18Z")

</div>

I found a way to load the named graph into the "Models tab" in Stardog Studio.

 ![image](https://canada1.discourse-cdn.com/flex030/uploads/stardog/original/2X/0/0220c5eded842ca6340a2de28602771944e14dcd.png)

 ![image](https://canada1.discourse-cdn.com/flex030/uploads/stardog/original/2X/c/c50df2a1984df6f3746ec181004a36a945447806.png)

Is there a programmatic way to go about this?

---

<div class="post-metadata">

**Author:** ![hall00ween](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.stardog.com/hall00ween/32/1752_2.png) [@hall00ween](https://community.stardog.com/u/hall00ween)\
**Post date:** [July 27, 2023, 5:37pm UTC](https://community.stardog.com/t/download-model-via-pystardog-or-http-api/4450/6 "2023-07-27T17:37:36Z")

</div>

I haven't found a programmatic way to go about this. My solution will likely be using the UI to load it...into the UI interface.
