Exemple #1
0
def _parse_project(project, filename, new_project):
    """Perform the required checks and get the final project name"""
    current_slug = get_cached_slug()

    # Check for existing project in-memory or in .jovianrc
    if not new_project and project is None:
        # From in-memory variable
        if current_slug is not None:
            project = current_slug
        # From .jovianrc file
        else:
            project = get_notebook_slug(filename)

    # Skip if project is not provided & can't be read
    if project is None:
        return None, None

    # Get project metadata for UUID & username/title
    if is_uuid(project):
        project_title = None
        metadata = api.get_gist(project)
    elif '/' in project:
        project_title = project.split('/')[1]
        username = api.get_current_user()['username']
        metadata = api.get_gist(project)
    # Attach username to the title
    else:
        project_title = project
        username = api.get_current_user()['username']
        metadata = api.get_gist(username + '/' + project)

    # Skip if metadata could not be found
    if not metadata:
        log('Creating a new project "' + username + '/' + project_title + '"')
        return project_title, None

    # Extract information from metadata
    username = metadata['owner']['username']
    project_title = metadata['title']
    project_id = metadata['slug']

    # Check if the current user can commit to this project
    permissions = api.get_gist_access(project_id)
    if not permissions['write']:
        return project_title, None

    # Log whether this is an update or creation
    if project_id is None:
        log('Creating a new notebook on ' + read_webapp_url())
    else:
        log('Updating notebook "' + username + "/" + project_title + '" on ' +
            read_webapp_url())

    return project_title, project_id
Exemple #2
0
    def test_ensure_org_some_creds_exist_default_org_id(
            self, mock_is_flavor_pro, mock_request_org_id, mock_requests_get):
        with fake_creds('.jovian-some-creds', 'credentials.json'):
            # setUp
            creds = {
                "WEBAPP_URL": "https://staging.jovian.ml/",
                "GUEST_KEY": "b6538d4dfde04fcf993463a828a9cec6",
                "API_URL": "https://api-staging.jovian.ai"
            }
            write_creds(creds)

            ensure_org()

            assert read_api_url() == "https://api.jovian.ai"
            assert read_org_id() == "public"
            assert read_webapp_url() == "https://jovian.ml/"
Exemple #3
0
def submit(assignment=None, notebook_url=None, **kwargs):
    """ Performs jovian.commit and makes a assignment submission with the uploaded notebook.
    """
    if not assignment:
        log("Please provide assignment name", error=True)
        return

    filename = _parse_filename(kwargs.get('filename'))
    if filename == '__notebook_source__.ipynb':
        log("""jovian.submit does not support kaggle notebooks directly.
         Please make a commit first, copy the notebook URL and pass it to jovian.submit.
         eg. jovian.submit(assignment="zero-to-pandas-a1", 
                           notebook_url="https://jovian.ai/PrajwalPrashanth/assignment")""", error=True)
        return

    post_url = POST_API.format(assignment)
    nb_url = notebook_url if notebook_url else commit(**kwargs)

    if nb_url:
        data = {
            'assignment_url': nb_url
        }
        auth_headers = _h()

        log('Submitting assignment..')
        res = post(url=_u(post_url),
                   json=data,
                   headers=auth_headers)

        if res.status_code == 200:
            data = res.json()['data']
            course_slug = data.get('course_slug')
            assignment_slug = data.get('section_slug')

            assignment_page_url = ASSIGNMENT_PAGE_URL.format(course_slug, assignment_slug)
            log('Verify your submission at {}'.format(urljoin(read_webapp_url(), assignment_page_url)))
        else:
            log('Jovian submit failed. {}'.format(pretty(res)), error=True)
def test_read_webapp_url():
    with fake_creds():
        write_webapp_url("http://fake-webapp-url.ai/")
        assert read_webapp_url() == "http://fake-webapp-url.ai/"
Exemple #5
0
def commit(message=None,
           files=[],
           outputs=[],
           environment='auto',
           privacy='auto',
           filename=None,
           project=None,
           new_project=None,
           git_commit=False,
           git_message='auto',
           **kwargs):
    """Uploads the current file (Jupyter notebook or python script) to |Jovian|

    Saves the checkpoint of the notebook, captures the required dependencies from 
    the python environment and uploads the notebook, env file, additional files like scripts, csv etc.
    to |Jovian|. Capturing the python environment ensures that the notebook can be reproduced.

    Args:
        message(string, optional): A short message to be used as the title for this version.

        files(array, optional): Any additional scripts(.py files), CSVs etc. that are required to
            run the notebook. These will be available in the files tab of the project page on Jovian.ml

        outputs(array, optional): Any outputs files or artifacts generated from the modeling processing.
            This can include model weights/checkpoints, generated CSVs, output images etc.

        environment(string, optional): The type of Python environment to be captured.  Allowed options are
            'conda' , 'pip', 'auto' (for automatic detection) and None (to skip environment capture).

        privacy(bool, optional): Privacy level of the project (if a new one is being created).

            * 'auto' - use account level settings. Defaults to 'public'
            * 'public' - visible on profile and publicly accessible/searchable
            * 'secret' - not on profile only accessible via the direct link
            * 'private' - only for the accessible to owner and collaborators

            This argument has no effect on existing project. Change the privacy settings of a existing notebook 
            on the webapp.

        filename(string, optional): The filename of the current Jupyter notebook or Python script. This is 
            detected automatically in most cases, but in certain environments like Jupyter Lab or password protected notebooks, the detection 
            may fail and the filename needs to be provided using this argument.


        project(string, optional): Name of the |Jovian| project to which the current notebook/file should 
            be committed. Format: 'username/title' e.g. 'aakashns/jovian-example' or 'jovian-example' 
            (username of current user inferred automatically). If the project does not exist, a new one is 
            created. If it exists, the current notebook is added as a new version to the existing project, if 
            you are a owner/collaborator. If left empty, project name is picked up from the `.jovianrc` file in the 
            current directory, or a new project is created using the filename as the project name. 

        new_project(bool, optional): Whether to create a new project or update the existing one. Allowed option 
            are False (use the existing project, if a .jovianrc file exists, if available), True (create a new project)

        git_commit(bool, optional): If True, also performs a Git commit and records the commit hash. This is 
            applicable only when the notebook is inside a Git repository.

        git_message(string, optional): Commit message for git. If not provided, it uses the `message` argument

    .. attention::
        Pass notebook's name to `filename` argument, in certain environments like Jupyter Lab and password protected 
        notebooks sometimes it may fail to detect notebook automatically.
    .. |Jovian| raw:: html

        <a href="https://jovian.ml/?utm_source=docs" target="_blank"> Jovian.ml </a>
    """
    global _current_slug

    # Deprecated argument (secret)
    if privacy == 'auto' and 'secret' in kwargs:
        privacy = 'secret' if kwargs['secret'] else 'auto'
        log('"secret" is deprecated. Use "privacy" instead (allowed options: ' +
            '"public", "private", "secret", "auto")', error=True)

    # Deprecated argument (nb_filename)
    if filename is None and 'nb_filename' in kwargs:
        filename = kwargs['nb_filename']
        log('"nb_filename" is deprecated. Use "filename" instead', error=True)

    # Deprecated argument (env_type)
    if 'env_type' in kwargs:
        environment = kwargs['environment']
        log('"env_type" is deprecated. Use "environment" instead', error=True)

    # Deprecated argument (capture_env)
    if 'capture_env' in kwargs and not kwargs['capture_env']:
        environment = None
        log('"catpure_env" is deprecated. Use "environment=None" instead', error=True)

    # Deprecated argument (notebook_id)
    if 'notebook_id' in kwargs:
        project = kwargs['notebook_id']
        log('"notebook_id" is deprecated. Use "project" instead.', error=True)

    # Deprecated argument (create_new)
    if 'create_new' in kwargs:
        new_project = kwargs['create_new']
        log('"create_new" is deprecated. Use "new_project" instead.', error=True)

    # Deprecated argument (artifacts)
    if 'artifacts' in kwargs:
        outputs = kwargs['artifacts']
        log('"artifacts" is deprecated. Use "outputs" instead', error=True)

    # Skip if unsupported environment
    if not in_script() and not in_notebook():
        log('Failed to detect Jupyter notebook or Python script. Skipping..', error=True)
        return

    # Attempt to save Jupyter notebook
    if in_notebook():
        save_notebook()
        log('Attempting to save notebook..')
        sleep(1)

    # Extract notebook/script filename
    filename = _parse_filename(filename)
    if filename is None:
        log(FILENAME_MSG)
        return

    # Ensure that the file exists
    if not os.path.exists(filename):
        log('The detected/provided file "' + filename +
            '" does not exist. Please provide the correct notebook filename ' +
            'as the "filename" argument to "jovian.commit".')
        return

    # Retrieve Gist ID & title
    project_title, project_id = _parse_project(project, filename, new_project)

    # Create or update gist (with title and )
    res = api.create_gist_simple(filename, project_id, privacy, project_title, message)
    slug, owner, version, title = res['slug'], res['owner'], res['version'], res['title']
    username = owner['username']

    # Cache slug for further commits
    _current_slug = slug
    set_notebook_slug(filename, slug)

    # Attach environment, files and outputs
    _capture_environment(environment, slug, version)
    _attach_files(files, slug, version)
    _attach_files(outputs, slug, version, output=True)

    if not git_message or git_message == 'auto':
        git_message = message or 'jovian commit ' + username + '/' + title + ' v' + str(version)
    _perform_git_commit(filename, git_commit, git_message)
    _attach_records(slug, version)

    log('Committed successfully! ' + read_webapp_url() + username + "/" + title)
Exemple #6
0
def test_read_webapp_url():
    with fake_creds('.jovian-write-api-url', 'credentials.json'):
        write_webapp_url("http://fake-webapp-url.ai/")
        assert read_webapp_url() == "http://fake-webapp-url.ai/"