<commit_before>import requests
import json
import webbrowser
import urllib


class Visualization(object):

    def __init__(self, session=None, json=None):
        self.session = session
        self.id = json.get('id')

    def _format_url(self, url):
        if not url.endswith('/'):
            url += '/'

        return url + '?host=' + urllib.quote(self.session.host)

    def append_image(self, image):
        url = self.session.host + '/sessions/' + str(self.session.id) + '/visualizations/' + str(self.id) + '/data/images'
        url = self._format_url(url)
        files = {'file': image}
        return requests.post(url, files=files, data={'type': 'image'})

    def append_data(self, data=None, field=None):
        payload = {'data': data}
        headers = {'Content-type': 'application/json', 'Accept': 'text/plain'}        
        url = self.session.host + '/sessions/' + str(self.session.id) + '/visualizations/' + str(self.id) + '/data/'
        if field:
            url += field

        url = self._format_url(url)
        return requests.post(url, data=json.dumps(payload), headers=headers)

    def update_data(self, data=None, field=None):
        payload = {'data': data}
        headers = {'Content-type': 'application/json', 'Accept': 'text/plain'}        
        url = self.session.host + '/sessions/' + str(self.session.id) + '/visualizations/' + str(self.id) + '/data/'
        if field:
            url += field

        url = self._format_url(url)
        return requests.put(url, data=json.dumps(payload), headers=headers)

    def get_permalink(self):
        return self.session.host + '/visualizations/' + str(self.id)

    def get_embed_link(self):
        return self._format_url(self.get_permalink() + '/embed')

    def get_html(self):
        import urllib2

        response = urllib2.urlopen(self.get_embed_link())

        return response.read()

    def open(self):
        webbrowser.open(self.session.host + '/visualizations/' + str(self.id) + '/')

    @classmethod
    def create(cls, session=None, data=None, images=None, type=None, auth=None):
        url = session.host + '/sessions/' + str(session.id) + '/visualizations'

        if not images:
            payload = {'data': data, 'type': type}
            headers = {'Content-type': 'application/json', 'Accept': 'text/plain'}

            r = requests.post(url, data=json.dumps(payload), headers=headers, auth=session.auth)

            if not r.status_code == requests.codes.ok:
                raise Exception('Problem uploading data')

            viz = cls(session=session, json=r.json())

        else:
            if not type:<commit_msg>add delete method on visualizations<commit_after>import requests
import json
import webbrowser
import urllib


class Visualization(object):

    def __init__(self, session=None, json=None):
        self.session = session
        self.id = json.get('id')

    def _format_url(self, url):
        if not url.endswith('/'):
            url += '/'

        return url + '?host=' + urllib.quote(self.session.host)

    def append_image(self, image):
        url = self.session.host + '/sessions/' + str(self.session.id) + '/visualizations/' + str(self.id) + '/data/images'
        url = self._format_url(url)
        files = {'file': image}
        return requests.post(url, files=files, data={'type': 'image'})

    def append_data(self, data=None, field=None):
        payload = {'data': data}
        headers = {'Content-type': 'application/json', 'Accept': 'text/plain'}        
        url = self.session.host + '/sessions/' + str(self.session.id) + '/visualizations/' + str(self.id) + '/data/'
        if field:
            url += field

        url = self._format_url(url)
        return requests.post(url, data=json.dumps(payload), headers=headers)

    def update_data(self, data=None, field=None):
        payload = {'data': data}
        headers = {'Content-type': 'application/json', 'Accept': 'text/plain'}        
        url = self.session.host + '/sessions/' + str(self.session.id) + '/visualizations/' + str(self.id) + '/data/'
        if field:
            url += field

        url = self._format_url(url)
        return requests.put(url, data=json.dumps(payload), headers=headers)

    def get_permalink(self):
        return self.session.host + '/visualizations/' + str(self.id)

    def get_embed_link(self):
        return self._format_url(self.get_permalink() + '/embed')

    def get_html(self):
        import urllib2

        response = urllib2.urlopen(self.get_embed_link())

        return response.read()

    def open(self):
        webbrowser.open(self.session.host + '/visualizations/' + str(self.id) + '/')

    def delete(self):
        url = self.get_permalink()
        return requests.delete(url)


    @classmethod
    def create(cls, session=None, data=None, images=None, type=None, auth=None):
        url = session.host + '/sessions/' + str(session.id) + '/visualizations'

        if not images:
            payload = {'data': data, 'type': type}
            headers = {'Content-type': 'application/json', 'Accept': 'text/plain'}

            r = requests.post(url, data=json.dumps(payload), headers=headers, auth=session.auth)

            if not r.status_code == requests.codes.ok:
                raise Exception('Problem uploading data')

            viz = cls(session=session, json=r.json())

        else:
            if not type:
{'fraction_non_alphanumeric': 0.15098508561959123, 'fraction_numerical': 0.0007365126127784938, 'mean_word_length': 2.518134715025907, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5907691', 'n_tokens_mistral': 1686, 'n_tokens_neox': 1623, 'n_words': 363}