<commit_before>// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
'use strict';
import '../common/extensions';

import { nbformat } from '@jupyterlab/coreutils';
import { Kernel, KernelMessage, ServerConnection, Session, SessionManager } from '@jupyterlab/services';
import * as fs from 'fs-extra';
import { inject, injectable } from 'inversify';
import * as path from 'path';
import { Observable } from 'rxjs/Observable';
import * as uuid from 'uuid/v4';
import * as vscode from 'vscode';

import { IFileSystem } from '../common/platform/types';
import { IDisposableRegistry, ILogger } from '../common/types';
import { createDeferred } from '../common/utils/async';
import * as localize from '../common/utils/localize';
import { RegExpValues } from './constants';
import { CellState, ICell, IJupyterExecution, INotebookProcess, INotebookServer } from './types';

// This code is based on the examples here:
// https://www.npmjs.com/package/@jupyterlab/services

@injectable()
export class JupyterServer implements INotebookServer {
    public isDisposed: boolean = false;
    private session: Session.ISession | undefined;
    private sessionManager : SessionManager | undefined;
    private sessionStartTime: number | undefined;
    private tempFile: string | undefined;
    private onStatusChangedEvent : vscode.EventEmitter<boolean> = new vscode.EventEmitter<boolean>();

    constructor(
        @inject(ILogger) private logger: ILogger,
        @inject(INotebookProcess) private process: INotebookProcess,
        @inject(IFileSystem) private fileSystem: IFileSystem,
        @inject(IDisposableRegistry) private disposableRegistry: IDisposableRegistry,
        @inject(IJupyterExecution) private jupyterExecution : IJupyterExecution) {
    }

    public start = async () : Promise<boolean> => {

        if (await this.jupyterExecution.isNotebookSupported()) {

            // First generate a temporary notebook. We need this as input to the session
            this.tempFile = await this.generateTempFile();

            // start our process in the same directory as our ipynb file.
            await this.process.start(path.dirname(this.tempFile));

            // Wait for connection information. We'll stick that into the options
            const connInfo = await this.process.waitForConnectionInformation();

            // First connect to the sesssion manager and find a kernel that matches our
            // python we're using
            const serverSettings = ServerConnection.makeSettings(
                {
                    baseUrl: connInfo.baseUrl,
                    token: connInfo.token,
                    pageUrl: '',
                    // A web socket is required to allow token authentication
                    wsUrl: connInfo.baseUrl.replace('http', 'ws'),
                    init: { cache: 'no-store', credentials: 'same-origin' }
                });
            this.sessionManager = new SessionManager({ serverSettings: serverSettings });

            // Ask Jupyter for its list of kernel specs.
            const kernelName = await this.findKernelName(this.sessionManager);

            // Create our session options using this temporary notebook and our connection info
            const options: Session.IOptions = {
                path: this.tempFile,
                kernelName: kernelName,
                serverSettings: serverSettings
            };

            // Start a new session
            this.session = await this.sessionManager.startNew(options);

            // Setup our start time. We reject anything that comes in before this time during execute
            this.sessionStartTime = Date.now();

            // Wait for it to be ready
            await this.session.kernel.ready;

            // Setup the default imports (this should be configurable in the future)
            this.executeSilently(
                'import pandas as pd\r\nimport numpy\r\n%matplotlib inline\r\nimport matplotlib.pyplot as plt'
            ).ignoreErrors();

            return true;
        } else {
            throw localize.DataScience.jupyterNotSupported();
        }

    }
<commit_msg>In vscode default dark theme default the matplot lib style to dark_background (#3230)<commit_after>// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
'use strict';
import '../common/extensions';

import { nbformat } from '@jupyterlab/coreutils';
import { Kernel, KernelMessage, ServerConnection, Session, SessionManager } from '@jupyterlab/services';
import * as fs from 'fs-extra';
import { inject, injectable } from 'inversify';
import * as path from 'path';
import { Observable } from 'rxjs/Observable';
import * as uuid from 'uuid/v4';
import * as vscode from 'vscode';

import { IWorkspaceService } from '../common/application/types';
import { IFileSystem } from '../common/platform/types';
import { IDisposableRegistry, ILogger } from '../common/types';
import { createDeferred } from '../common/utils/async';
import * as localize from '../common/utils/localize';
import { RegExpValues } from './constants';
import { CellState, ICell, IJupyterExecution, INotebookProcess, INotebookServer } from './types';

// This code is based on the examples here:
// https://www.npmjs.com/package/@jupyterlab/services

@injectable()
export class JupyterServer implements INotebookServer {
    public isDisposed: boolean = false;
    private session: Session.ISession | undefined;
    private sessionManager : SessionManager | undefined;
    private sessionStartTime: number | undefined;
    private tempFile: string | undefined;
    private onStatusChangedEvent : vscode.EventEmitter<boolean> = new vscode.EventEmitter<boolean>();

    constructor(
        @inject(ILogger) private logger: ILogger,
        @inject(INotebookProcess) private process: INotebookProcess,
        @inject(IFileSystem) private fileSystem: IFileSystem,
        @inject(IDisposableRegistry) private disposableRegistry: IDisposableRegistry,
        @inject(IJupyterExecution) private jupyterExecution : IJupyterExecution,
        @inject(IWorkspaceService) private workspaceService: IWorkspaceService) {
    }

    public start = async () : Promise<boolean> => {

        if (await this.jupyterExecution.isNotebookSupported()) {

            // First generate a temporary notebook. We need this as input to the session
            this.tempFile = await this.generateTempFile();

            // start our process in the same directory as our ipynb file.
            await this.process.start(path.dirname(this.tempFile));

            // Wait for connection information. We'll stick that into the options
            const connInfo = await this.process.waitForConnectionInformation();

            // First connect to the sesssion manager and find a kernel that matches our
            // python we're using
            const serverSettings = ServerConnection.makeSettings(
                {
                    baseUrl: connInfo.baseUrl,
                    token: connInfo.token,
                    pageUrl: '',
                    // A web socket is required to allow token authentication
                    wsUrl: connInfo.baseUrl.replace('http', 'ws'),
                    init: { cache: 'no-store', credentials: 'same-origin' }
                });
            this.sessionManager = new SessionManager({ serverSettings: serverSettings });

            // Ask Jupyter for its list of kernel specs.
            const kernelName = await this.findKernelName(this.sessionManager);

            // Create our session options using this temporary notebook and our connection info
            const options: Session.IOptions = {
                path: this.tempFile,
                kernelName: kernelName,
                serverSettings: serverSettings
            };

            // Start a new session
            this.session = await this.sessionManager.startNew(options);

            // Setup our start time. We reject anything that comes in before this time during execute
            this.sessionStartTime = Date.now();

            // Wait for it to be ready
            await this.session.kernel.ready;

            // Check for dark theme, if so set matplot lib to use dark_background settings
            let darkTheme: boolean = false;
            const workbench = this.workspaceService.getConfiguration('workbench');
            if (workbench) {
                const theme = workbench.get<string>('colorTheme');
                if (theme) {
                    darkTheme = /dark/i.test(theme);
                }
            }

            this.executeSilently(
                `import pandas as pd\r\nimport numpy\r\n%matplotlib inline\r\nimport matplotlib.pyplot as plt${darkTheme ? '\r\nfrom matplotlib import style\r\nstyle.use(\'dark_background\')' : ''}`
            ).ignoreErrors();

            return true;
        } else {
            throw localize.DataScience.jupyterNotSupported();
        }

    }

{'fraction_non_alphanumeric': 0.09218836565096952, 'fraction_numerical': 0.000664819944598338, 'mean_word_length': 2.507967353284104, 'pattern_counts': {'":': 0, '<': 10, '<?xml version=': 0, '>': 12, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, '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': '2258580', 'n_tokens_mistral': 2312, 'n_tokens_neox': 2236, 'n_words': 716}