Skip to content

adding functionality to write to datasets #18

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,23 @@ SurveyCTOObject(server_name,
</p>


*
```python
upload_dataset()
```
<p>Upload a pandas df to a dataset

*Parameters:*
- **df** *(pd.DataFrame)*: pandas DataFrame to upload
- **dataset_id** *(str)*: ID of the dataset on SurveyCTO
- **dataset_title** *(str)*: Optional title for the dataset (defaults to dataset_id)
- **append** *(bool)*: If True, appends data; otherwise replaces the dataset
- **fill** *(bool)*: If True, allows mismatched columns in append mode

*Returns:* JSON response message from server
</p>


<a name="usecases"></a>
# Use Cases

Expand Down
16 changes: 16 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,22 @@ Methods:

*Returns:* list of dictionaries, each dictionary containing information for each form on server

-
.. code:: python

upload_dataset()

Upload a pandas df to a dataset

*Parameters:*
- **df** *(pd.DataFrame)*: pandas DataFrame to upload
- **dataset_id** *(str)*: ID of the dataset on SurveyCTO
- **dataset_title** *(str)*: Optional title for the dataset (defaults to dataset_id)
- **append** *(bool)*: If True, appends data; otherwise replaces the dataset
- **fill** *(bool)*: If True, allows mismatched columns in append mode

*Returns:* JSON response message from server


Use Cases
=========
Expand Down
72 changes: 71 additions & 1 deletion pysurveycto/pysurveycto.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@
import datetime
import warnings
from urllib.parse import quote

import pandas as pd
import tempfile
import os
import requests

class IllegalArgumentError(ValueError):
"""
Expand Down Expand Up @@ -724,3 +727,70 @@ def list_forms(self):
raise e

return response.json()['forms']

def upload_dataset(self, df, dataset_id, dataset_title=None, append=False, fill=False):
"""
Uploads a pandas df to a dataset
:return: dictionary with previous dataset preview and upload response

:param df: pandas DataFrame to upload
:param dataset_id: ID of the dataset on SurveyCTO
:param dataset_title: Optional title for the dataset (defaults to dataset_id)
:param append: If True, appends data; otherwise replaces the dataset
:param fill: If True, allows mismatched columns in append mode
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like this parameter isn't getting used in the function. Can we remove it?
In case of mismatched columns does the API return an error?


"""

assert isinstance(df, pd.DataFrame), "data must be a pandas DataFrame"
assert isinstance(dataset_id, str), "dataset_id must be a string"
if dataset_title is None:
dataset_title = dataset_id
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the dataset_title is not needed for the API, I think we can remove this from the params as well. There is no check that the title is correct and seems redundant. What do you you think?


headers = self.__auth()

try:
check_resp = self.get_server_dataset(dataset_id)
except requests.exceptions.RequestException as e:
raise RuntimeError(f"Failed to retrieve dataset: {e}")

dataset_upload_mode = 'append' if append else 'clear'
dataset_exists = 1
dataset_type = 'SERVER'

with tempfile.NamedTemporaryFile(suffix=".csv", delete=False, mode='w', newline='') as tmp:
df.to_csv(tmp.name, index=False)
tmp_path = tmp.name

upload_url = (
f"https://{self.server_name}.surveycto.com/"
f'datasets/{dataset_id}/upload?csrf_token=' + '{headers[' + 'X-csrf-token' + ']}'
)

with open(tmp_path, 'rb') as f:
files = {
'dataset_file': (os.path.basename(tmp_path), f, 'text/csv')
}
payload = {
'dataset_exists': dataset_exists,
'dataset_id': dataset_id,
'dataset_title': dataset_title,
'dataset_upload_mode': dataset_upload_mode,
'dataset_type': dataset_type,
}

try:
upload_resp = self._sesh.post(
upload_url,
data=payload,
files=files,
cookies=self._sesh.cookies,
headers=headers
)
upload_resp.raise_for_status()
finally:
os.remove(tmp_path)

return {

'response': upload_resp.json()
}