Skip to content

Commit

Permalink
Merge pull request #70 from kedro-org/viz/viz-py-package
Browse files Browse the repository at this point in the history
Remove kedro-viz backend server, fetch data directly instead
  • Loading branch information
noklam authored Aug 28, 2024
2 parents 7b5b2a8 + a17a1fa commit ff43122
Show file tree
Hide file tree
Showing 9 changed files with 64 additions and 75 deletions.
17 changes: 17 additions & 0 deletions bundled/tool/lsp_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@
)
from pygls.server import LanguageServer

from kedro_viz.server import load_and_populate_data
from kedro_viz.api.rest.responses import get_kedro_project_json_data

class KedroLanguageServer(LanguageServer):
"""Store Kedro-specific information in the language server."""
Expand Down Expand Up @@ -553,6 +555,21 @@ def definition_from_flowchart(ls, word):
result = definition(LSP_SERVER, params=None, word=word)
return result

@LSP_SERVER.command("kedro.getProjectData")
def get_porject_data_from_viz(lsClient):
"""Get project data from kedro viz
"""
data = None
try:
load_and_populate_data(Path.cwd())
data = get_kedro_project_json_data()
return data
except Exception as e:
print(f"An error occurred: {e}")
finally:
print("Execution completed.")
return data

### End of kedro-lsp


Expand Down
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,6 @@
"dependencies": {
"@vscode/python-extension": "^1.0.5",
"fs-extra": "^11.2.0",
"node-fetch": "^3.3.2",
"vscode-languageclient": "^8.1.0"
},
"devDependencies": {
Expand Down
17 changes: 16 additions & 1 deletion src/common/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,6 @@ export async function executeServerDefinitionCommand(lsClient: LanguageClient |
const result: any[] | undefined = await vscode.commands.executeCommand(
commandName /* if your command accepts arguments you can pass them here */,
target,

);
logger.info(`${commandName} result: ${JSON.stringify(result, undefined, 2)}`);
if (result && result.length > 0) {
Expand All @@ -99,3 +98,19 @@ export async function executeServerDefinitionCommand(lsClient: LanguageClient |
}
}

export async function executeGetProjectDataCommand(lsClient: LanguageClient | undefined) {
if (!lsClient || lsClient.state !== State.Running) {
await vscode.window.showErrorMessage('There is no language server running.');
return;
}
if (!lsClient.initializeResult) {
await vscode.window.showErrorMessage('The Language Server fail to initialise.');
return;
}

const commandName = 'kedro.getProjectData';
logger.info(`executing command: '${commandName}'`);
const result = await vscode.commands.executeCommand(commandName);
return result;
}

8 changes: 0 additions & 8 deletions src/common/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import * as path from 'path';
import { LogLevel, Uri, WorkspaceFolder } from 'vscode';
import { Trace } from 'vscode-jsonrpc/node';
import { getWorkspaceFolders } from './vscodeapi';
import fetch from 'node-fetch';
import KedroVizPanel from '../webview/vizWebView';

function logLevelToTrace(logLevel: LogLevel): Trace {
Expand Down Expand Up @@ -67,10 +66,3 @@ export async function getProjectRoot(): Promise<WorkspaceFolder> {
return rootWorkspace;
}
}

export async function fetchAndUpdateProjectData(): Promise<void> {
fetch('http://127.0.0.1:3131/api/main')
.then((response: { text: () => any }) => response.text())
.then((data: string) => KedroVizPanel.currentPanel?.updateData(data))
.catch((err: { message: string }) => console.error('Error: ' + err.message));
}
38 changes: 23 additions & 15 deletions src/extension.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
import {
selectEnvironment,
executeServerCommand,
executeServerDefinitionCommand,
executeGetProjectDataCommand,
} from './common/commands';

import * as vscode from 'vscode';
import { selectEnvironment, executeServerCommand, executeServerDefinitionCommand } from './common/commands';
import { LanguageClient } from 'vscode-languageclient/node';
import { registerLogger, traceError, traceLog, traceVerbose } from './common/log/logging';
import {
Expand All @@ -18,10 +24,13 @@ import { createStatusBar } from './common/status_bar';
import { getLSClientTraceLevel } from './common/utilities';
import { createOutputChannel, onDidChangeConfiguration, registerCommand } from './common/vscodeapi';
import KedroVizPanel from './webview/vizWebView';
import { runKedroVizServer } from './webview/vizServer';

let lsClient: LanguageClient | undefined;
let kedroVizProcess: any;

export async function getlsClient() {
return lsClient;
}


export async function activate(context: vscode.ExtensionContext): Promise<void> {
// This is required to get server name and module. This should be
Expand Down Expand Up @@ -62,6 +71,15 @@ export async function activate(context: vscode.ExtensionContext): Promise<void>
);


context.subscriptions.push(
vscode.commands.registerCommand('kedro.runKedroViz', async () => {
KedroVizPanel.createOrShow(context.extensionUri);
const projectData = await executeGetProjectDataCommand(lsClient);
KedroVizPanel.currentPanel?.updateData(projectData);
}),
);


// Log Server information
traceLog(`Name: ${serverInfo.name}`);
traceLog(`Module: ${serverInfo.module}`);
Expand All @@ -87,12 +105,6 @@ export async function activate(context: vscode.ExtensionContext): Promise<void>
console.log(interpreterDetails);
console.log('===============DEBUG============');

// Start kedro viz server
if (kedroVizProcess) {
process.kill(-kedroVizProcess.pid);
}
kedroVizProcess = await runKedroVizServer();

if (interpreterDetails.path) {
traceVerbose(`Using interpreter from Python extension: ${interpreterDetails.path.join(' ')}`);
lsClient = await restartServer(serverId, serverName, outputChannel, lsClient, env);
Expand Down Expand Up @@ -129,8 +141,8 @@ export async function activate(context: vscode.ExtensionContext): Promise<void>
registerCommand('pygls.server.executeCommand', async () => {
await executeServerCommand(lsClient);
}),
registerCommand('kedro.sendDefinitionRequest', async () => {
await executeServerDefinitionCommand(lsClient);
registerCommand('kedro.sendDefinitionRequest', async (word) => {
await executeServerDefinitionCommand(lsClient, word);
}),
vscode.commands.registerCommand('kedro.runKedroViz', () => {
KedroVizPanel.createOrShow(context.extensionUri, lsClient);
Expand All @@ -154,8 +166,4 @@ export async function deactivate(): Promise<void> {
if (lsClient) {
await lsClient.stop();
}
if (kedroVizProcess) {
process.kill(-kedroVizProcess.pid);
kedroVizProcess = null; // Reset the reference after killing the process
}
}
3 changes: 2 additions & 1 deletion src/webview/goToDefinition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ export async function goToDefinition(message: Message) {
let filePattern = '**/*.yml';

if (message.type === 'task') {
filePattern = '**/*.py';
// Looking only in pipelines folders
filePattern = '**/pipelines/**/*.py';
}

const files = await vscode.workspace.findFiles(filePattern);
Expand Down
36 changes: 0 additions & 36 deletions src/webview/vizServer.ts

This file was deleted.

17 changes: 5 additions & 12 deletions src/webview/vizWebView.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import * as vscode from 'vscode';
import fetch from 'node-fetch';
import { goToDefinition } from './goToDefinition';
import { fetchAndUpdateProjectData } from '../common/utilities';
import { LanguageClient } from 'vscode-languageclient/node';
import { executeServerDefinitionCommand } from '../common/commands';

Expand All @@ -21,7 +19,7 @@ export default class KedroVizPanel {
private readonly _extensionUri: vscode.Uri;
private _disposables: vscode.Disposable[] = [];

public static createOrShow(extensionUri: vscode.Uri, lsClient: LanguageClient | undefined) {
public static createOrShow(extensionUri: vscode.Uri) {
const column = vscode.window.activeTextEditor ? vscode.window.activeTextEditor.viewColumn : undefined;

// If we already have a panel, show it.
Expand All @@ -36,15 +34,14 @@ export default class KedroVizPanel {
retainContextWhenHidden: true,
});

KedroVizPanel.currentPanel = new KedroVizPanel(panel, extensionUri, lsClient);
fetchAndUpdateProjectData();
KedroVizPanel.currentPanel = new KedroVizPanel(panel, extensionUri);
}

public static revive(panel: vscode.WebviewPanel, extensionUri: vscode.Uri) {
KedroVizPanel.currentPanel = new KedroVizPanel(panel, extensionUri);
}

private constructor(panel: vscode.WebviewPanel, extensionUri: vscode.Uri, lsClient?: LanguageClient) {
private constructor(panel: vscode.WebviewPanel, extensionUri: vscode.Uri) {
this._panel = panel;
this._extensionUri = extensionUri;

Expand Down Expand Up @@ -72,7 +69,8 @@ export default class KedroVizPanel {
switch (message.command) {
case 'fromWebview':
if (message.node.type === 'data') {
await executeServerDefinitionCommand(lsClient, message.node.text);
// await executeServerDefinitionCommand(getlsClient(), message.node.text);
await vscode.commands.executeCommand('kedro.sendDefinitionRequest', message.node.text)
} else {
await goToDefinition(message.node);
}
Expand All @@ -84,11 +82,6 @@ export default class KedroVizPanel {
);
}

public updateTheme() {
// Send a message to the webview.
this._panel.webview.postMessage({ command: 'updateTheme', theme: 'light' });
}

public updateData(data: any) {
// Send a message to the webview.
this._panel.webview.postMessage({ command: 'updateData', data });
Expand Down
2 changes: 1 addition & 1 deletion webview/src/App.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ function App() {
const message = event.data;
switch (message.command) {
case "updateData":
setData(JSON.parse(message.data));
setData(message.data);
setLoading(false);
break;
default:
Expand Down

0 comments on commit ff43122

Please sign in to comment.