forked from DonJayamanne/pythonVSCode
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added export notebook to PDF (#12517)
* temp work * started working on dependency checking * good start on pdf * removed space from label * added to command pallete * changed pdf method to TeX * fixed pdf title * removed unecessary changes * changed back package-lock.json * cleaned up dependency checker * made changes and add telemetry * changed from 2 to 1 error message * changed name for temp directory * removed duplicate import * removed mardown prefix * deleted temp file
- Loading branch information
1 parent
3bb92e1
commit 036621a
Showing
11 changed files
with
138 additions
and
33 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,73 @@ | ||
import { injectable } from 'inversify'; | ||
import { inject, injectable } from 'inversify'; | ||
import * as path from 'path'; | ||
import { Uri } from 'vscode'; | ||
import { IFileSystem } from '../../common/platform/types'; | ||
import { IPythonExecutionFactory } from '../../common/process/types'; | ||
import { IJupyterSubCommandExecutionService, INotebookImporter } from '../types'; | ||
import { ExportBase } from './exportBase'; | ||
|
||
@injectable() | ||
export class ExportToPDF extends ExportBase { | ||
// tslint:disable-next-line: no-empty | ||
public async export(_source: Uri, _target: Uri): Promise<void> {} | ||
constructor( | ||
@inject(IPythonExecutionFactory) protected readonly pythonExecutionFactory: IPythonExecutionFactory, | ||
@inject(IJupyterSubCommandExecutionService) | ||
protected jupyterService: IJupyterSubCommandExecutionService, | ||
@inject(IFileSystem) protected readonly fileSystem: IFileSystem, | ||
@inject(INotebookImporter) protected readonly importer: INotebookImporter | ||
) { | ||
super(pythonExecutionFactory, jupyterService, fileSystem, importer); | ||
} | ||
|
||
public async export(source: Uri, target: Uri): Promise<void> { | ||
const tempFile = await this.fileSystem.createTemporaryFile('.ipynb'); | ||
const directoryPath = path.join( | ||
path.dirname(tempFile.filePath), | ||
path.basename(tempFile.filePath, path.extname(tempFile.filePath)) | ||
); | ||
tempFile.dispose(); | ||
const newFileName = path.basename(target.fsPath, path.extname(target.fsPath)); | ||
const newSource = Uri.file(await this.createNewFile(directoryPath, newFileName, source)); | ||
|
||
const args = [ | ||
newSource.fsPath, | ||
'--to', | ||
'pdf', | ||
'--output', | ||
path.basename(target.fsPath), | ||
'--output-dir', | ||
path.dirname(target.fsPath) | ||
]; | ||
try { | ||
await this.executeCommand(newSource, target, args); | ||
} finally { | ||
await this.deleteNewDirectory(directoryPath); | ||
} | ||
} | ||
|
||
private async createNewFile(dirPath: string, newName: string, source: Uri): Promise<string> { | ||
// When exporting to PDF we need to change the source files name to match | ||
// what the title of the pdf should be. | ||
// To ensure the new file path is unique we will create a directory and | ||
// save the new file there | ||
try { | ||
await this.fileSystem.createDirectory(dirPath); | ||
const newFilePath = path.join(dirPath, newName); | ||
await this.fileSystem.copyFile(source.fsPath, newFilePath); | ||
return newFilePath; | ||
} catch (e) { | ||
await this.deleteNewDirectory(dirPath); | ||
throw e; | ||
} | ||
} | ||
|
||
private async deleteNewDirectory(dirPath: string) { | ||
if (!(await this.fileSystem.directoryExists(dirPath))) { | ||
return; | ||
} | ||
const files = await this.fileSystem.getFiles(dirPath); | ||
for (const file of files) { | ||
await this.fileSystem.deleteFile(file); | ||
} | ||
await this.fileSystem.deleteDirectory(dirPath); | ||
} | ||
} |
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