Locally:
I have used relative path and ScriptEngine
and ScriptScope
classes:
using System;
using System.IO;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using Microsoft.Scripting.Hosting;
using IronPython.Hosting;
namespace KrishNet6PyFunc101
{
public static class Function1
{
private static ScriptScope pyscriptoutput;
[FunctionName("Function1")]
public static async Task<IActionResult> Run(
[HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = null)] HttpRequest req,
ILogger log)
{
log.LogInformation("C# HTTP trigger function processed a request.");
ScriptEngine engine = Python.CreateEngine();
ScriptScope scope = engine.CreateScope();
engine.ExecuteFile(".\\script.py", scope);
var result = scope.GetVariable("joined_string");
Console.WriteLine(result);
string responseMessage = "Hello Krishna, This HTTP triggered function executed successfully and Python Script Output is " +result;
return new OkObjectResult(responseMessage);
}
}
}

Azure Portal:
For the Azure Function App Portal deployed, I have changed the code accordingly and it worked:
Using the same ScriptEngine
and ScriptScope
classes but taken the path using FunctionAppDirectory
,
using System;
using System.IO;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using Microsoft.Scripting.Hosting;
using IronPython.Hosting;
namespace KrishNet6PyFunc101
{
public static class Function1
{
private static ScriptScope pyscriptoutput;
[FunctionName("Function1")]
public static async Task<IActionResult> Run(
[HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = null)] HttpRequest req,
ILogger log,ExecutionContext context)
{
log.LogInformation("C# HTTP trigger function processed a request.");
ScriptEngine engine = Python.CreateEngine();
ScriptScope scope = engine.CreateScope();
string lfile = Path.Combine(context.FunctionAppDirectory, "script.py");
Console.WriteLine("filepath:"+lfile);
engine.ExecuteFile(lfile,scope);
var result = scope.GetVariable("joined_string");
Console.WriteLine(result);
string responseMessage = "Hello Krishna, This HTTP triggered function executed successfully and Python Script Output is " +result;
return new OkObjectResult(responseMessage);
}
}
}
