forked from microsoft/vscode-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added export to PDF, HTML and python to interactive window (microsoft…
…#12732) * mostly working * fixed naming * added news file * added export util to clean up code * added export util to clean up code * added export util to service container * removed busy indicator * addressed comments * refactored and cleaned up * added sleep to temp file creation
- Loading branch information
1 parent
84a99ee
commit 1b3bebd
Showing
14 changed files
with
146 additions
and
74 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Added exporting to python, HTML and PDF from the interative window. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import { inject, injectable } from 'inversify'; | ||
import * as os from 'os'; | ||
import * as path from 'path'; | ||
import * as uuid from 'uuid/v4'; | ||
import { Uri } from 'vscode'; | ||
import { IFileSystem, TemporaryDirectory } from '../../common/platform/types'; | ||
import { sleep } from '../../common/utils/async'; | ||
import { ICell, IDataScienceErrorHandler, INotebookExporter, INotebookModel, INotebookStorage } from '../types'; | ||
|
||
@injectable() | ||
export class ExportUtil { | ||
constructor( | ||
@inject(IFileSystem) private fileSystem: IFileSystem, | ||
@inject(IDataScienceErrorHandler) private readonly errorHandler: IDataScienceErrorHandler, | ||
@inject(INotebookStorage) private notebookStorage: INotebookStorage, | ||
@inject(INotebookExporter) private jupyterExporter: INotebookExporter | ||
) {} | ||
|
||
public async generateTempDir(): Promise<TemporaryDirectory> { | ||
const resultDir = path.join(os.tmpdir(), uuid()); | ||
await this.fileSystem.createDirectory(resultDir); | ||
|
||
return { | ||
path: resultDir, | ||
dispose: async () => { | ||
// Try ten times. Process may still be up and running. | ||
// We don't want to do async as async dispose means it may never finish and then we don't | ||
// delete | ||
let count = 0; | ||
while (count < 10) { | ||
try { | ||
await this.fileSystem.deleteDirectory(resultDir); | ||
count = 10; | ||
} catch { | ||
await sleep(3000); | ||
count += 1; | ||
} | ||
} | ||
} | ||
}; | ||
} | ||
|
||
public async makeFileInDirectory(model: INotebookModel, fileName: string, dirPath: string): Promise<string> { | ||
const newFilePath = path.join(dirPath, fileName); | ||
|
||
try { | ||
const content = model ? model.getContent() : ''; | ||
await this.fileSystem.writeFile(newFilePath, content, 'utf-8'); | ||
} catch (e) { | ||
await this.errorHandler.handleError(e); | ||
} | ||
|
||
return newFilePath; | ||
} | ||
|
||
public async getModelFromCells(cells: ICell[]): Promise<INotebookModel> { | ||
const tempDir = await this.generateTempDir(); | ||
const tempFile = await this.fileSystem.createTemporaryFile('.ipynb'); | ||
let model: INotebookModel; | ||
|
||
try { | ||
await this.jupyterExporter.exportToFile(cells, tempFile.filePath, false); | ||
const newPath = path.join(tempDir.path, '.ipynb'); | ||
await this.fileSystem.copyFile(tempFile.filePath, newPath); | ||
model = await this.notebookStorage.load(Uri.file(newPath)); | ||
} finally { | ||
tempFile.dispose(); | ||
tempDir.dispose(); | ||
} | ||
|
||
return model; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters