how to run a python file in Typescript file

Viewed 34

I have a typescript extension that needs to run a python file. after a file dialog is opened the extension uses exec to run the following command.

python C:\\Users\\schriste\\.vscode\\extensions\\consolidatedExtension\\src\\testingFile.py C:\\EquipVsCode\\5G_ServingCell_Rsrp_Rsrq_Sinr_RSSI_B974_all.isf

I thought exec would work since ive tired exec using the windows command dir and it worked just fine.

all I need is the right function to run the command shown above. has anyone done this? it seems like such a simple and common task but im having so much difficulty finding information on how this can be done.

below is the code from extension.ts

perhaps someone has information on how the following work for what I am trying to accomplish but I have not found a way for these to work as of right now: execFile spawn

// The module 'vscode' contains the VS Code extensibility API
// Import the module and reference it with the alias vscode in your code below
import * as vscode from 'vscode';
import { open } from 'fs';
import * as path from 'path';
import { json } from 'stream/consumers';

const { ChildProcess, exec } = require('child_process');
const { execFile } = require('node:child_process');
const { spawn } = require('node:child_process');
const { stdout, stderr } = require('process');

// this method is called when your extension is activated
// your extension is activated the very first time the command is executed
export function activate(context: vscode.ExtensionContext) {
    
    //File Dialog
    let disposable = vscode.commands.registerCommand('consolidatedExtension.openFile', function () {

        const options = {
            canSelectMany: false,
            openLabel: 'Open'
       };
        vscode.window.showOpenDialog(options).then(fileUri => {
            if (fileUri && fileUri[0]) {
                console.log('Selected file: ' + fileUri[0].fsPath);
                
                let cmd = `python C:\\Users\\schriste\\.vscode\\extensions\\consolidatedExtension\\src\\testingFile.py` + ` ` + fileUri[0].fsPath
                
                
                exec(cmd2, (...args: any[])=>{
                 console.log(args);
                });
            }
            
        });
    
    });

    //open graphView 
    let openWebview = vscode.commands.registerCommand('consolidatedExtension.openWebview', () => {
        const panel = vscode.window.createWebviewPanel(
        'openWebview', // Identifies the type of the webview. Used internally
        'Example Page', // Title of the panel displayed to the user
        vscode.ViewColumn.One, // Editor column to show the new webview panel in.
        { // Enable scripts in the webview
            enableScripts: true //Set this to true if you want to enable Javascript. 
        });
    
        //get path to json on disk
        const onDiskPath = vscode.Uri.file(
            path.join(context.extensionPath, 'src', 'data.json')
        );
    
        //get vegaLite js on disk
        const JsonDiskPath = vscode.Uri.file(
            path.join(context.extensionPath, 'src', 'vegaLiteGraphs.js')
        );
    
        //and get the special URI to use with the webview
        const jsonSRC = panel.webview.asWebviewUri(onDiskPath);
        const jsSRC = panel.webview.asWebviewUri(JsonDiskPath);
    
        panel.webview.html = getWebviewContent(jsonSRC, jsSRC);
      });

    context.subscriptions.push(openWebview);
    context.subscriptions.push(disposable);
}

// this method is called when your extension is deactivated
export function deactivate() {}

function getWebviewContent(jsonSRC:vscode.Uri, jsSRC:vscode.Uri) {
    
    return `<!DOCTYPE html>
    <html lang="en">
      <head>
        <script src="https://cdn.jsdelivr.net/npm/vega@5.21.0"></script>
        <script src="https://cdn.jsdelivr.net/npm/vega-lite@5.2.0"></script>
        <script src="https://cdn.jsdelivr.net/npm/vega-embed@6.20.2"></script>
      </head>
      <body>
      <script type = "module">
        import users from '${jsonSRC}' assert {type: 'json'};
      
      const graphDataContainer = []
      let dataPoints = []
      
      for(let i = 0; i < users.length; i += 2)
      {
        for (let j = 1; j < users[i].length; j++) {
          dataPoints.push({
            x: users[i][j], 
            y: users[i + 1][j] 
              });
        }
        graphDataContainer.push(dataPoints)
        dataPoints = []
      }
    
      console.log(graphDataContainer)
    
    
      
          window.onload = function() {
            let i = 0
          var chart1 = new CanvasJS.Chart("chart1", {
              animationEnabled: true,
              zoomEnabled: true,
              theme: "light2",
              "title": { "text": "RSRP" },
              axisX: {  
                       gridThickness: 0,
                       tickLength: 0,
                      lineThickness: 0,
                      labelFormatter: function(){ return " "; },
                      crosshair: {
                                    enabled: true, 
                                    color: "black",
                                    labelFontColor: "#F8F8F8"
                                },                      
                //interval: 10,
                //intervalType: "millisecond",
                //valueFormatString: "##.##.####"
                      },
            axisY: {
                      crosshair: {
                    enabled: true,
                        color: "black",
                        labelFontColor: "#F8F8F8"
                },                          
                  title: "Serving Cell RSRP (dBm)",
                    minimum: -100,
                    maximum: 0,
                    interval: 16.67
              },
              data: [{
                  type: "line",
                  //yValueFormatString: "#,### Units",
                  dataPoints: graphDataContainer[i]
              }],
              rangeChanged: syncHandler
          });
    
          chart1.render();
          i++
          var rsrqChart = new CanvasJS.Chart("chart2", {
              animationEnabled: true,
              zoomEnabled: true,
              theme: "light2",
              axisX: {  
                valueFormatString:"##:##:##.###", 
                crosshair: {
                    enabled: true, 
                    color: "black",
                    labelFontColor: "#F8F8F8"
                },  
              },                    
            axisY: {    
                crosshair: {
                enabled: true,
                color: "black",
                labelFontColor: "#F8F8F8"
               },           
                  title: "Serving Cell RSRQ",
                minimum: -15, 
                interval: 2.5
              },
              data: [{
                  type: "line",
                  dataPoints: graphDataContainer[i]
              }],
              rangeChanged: syncHandler
          });
          rsrqChart.render();
          // var chart3 = new CanvasJS.Chart("chartContainer3", {
          //     animationEnabled: true,
          //     theme: "light2",
          //     data: [{
          //         type: "line",
          //         dataPoints: sinrPoints
          //     }]
          // });
          // chart3.render();
    
          var charts = [chart1, rsrqChart];
          function syncHandler(e){
            for (var i = 0; i < charts.length; i++) {
            var chart = charts[i];
     
            if (!chart.options.axisX) 
            chart.options.axisX = {};
            
            if (!chart.options.axisY) 
                chart.options.axisY = {};
     
            if (e.trigger === "reset") {
                
                chart.options.axisX.viewportMinimum = chart.options.axisX.viewportMaximum = null;
                chart.options.axisY.viewportMinimum = chart.options.axisY.viewportMaximum = null;
            
                chart.render();
        
            } else if (chart !== e.chart) {
                
                chart.options.axisX.viewportMinimum = e.axisX[0].viewportMinimum;
                chart.options.axisX.viewportMaximum = e.axisX[0].viewportMaximum;
                
                chart.options.axisY.viewportMinimum = e.axisY[0].viewportMinimum;
                chart.options.axisY.viewportMaximum = e.axisY[0].viewportMaximum;
     
                chart.render();
     
            }
        }
          }
      }
      </script>
      <script type = "module" src = "${jsSRC}"></script>
      <div id="chart1" style="height: 370px; width: 100%;"></div>
      <div id="chart2" style="height: 370px; width: 100%;"></div>
      <div id="vis"></div>
      <!-- <div id="chartContainer3" style="height: 370px; width: 100%;"></div> -->
      <script src="https://canvasjs.com/assets/script/canvasjs.min.js"> </script>
      </body>
    </html>
    `;
  }
0 Answers
Related