Skip to content

Commit 237a59b

Browse files
committed
Examples: added a very simple .NET Core Web API example
1 parent 7f8d1e9 commit 237a59b

8 files changed

Lines changed: 177 additions & 1 deletion

File tree

Numpy.NET.sln

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Numpy.Bare.UnitTest", "test
1717
EndProject
1818
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ReleaseBot", "src\ReleaseBot\ReleaseBot.csproj", "{2BAEA60C-88A2-45DC-8044-2C9571E1B8CF}"
1919
EndProject
20-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NeuralNetworkExample", "NeuralNetworkExample\NeuralNetworkExample.csproj", "{66A030C3-69B8-4A84-9F4B-308D66CF4FD3}"
20+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NeuralNetworkExample", "NeuralNetworkExample\NeuralNetworkExample.csproj", "{66A030C3-69B8-4A84-9F4B-308D66CF4FD3}"
21+
EndProject
22+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WebApiExample", "WebApiExample\WebApiExample.csproj", "{7C23FCEB-BFC1-4720-87E1-004FA59A0FBC}"
2123
EndProject
2224
Global
2325
GlobalSection(SolutionConfigurationPlatforms) = preSolution
@@ -53,13 +55,18 @@ Global
5355
{66A030C3-69B8-4A84-9F4B-308D66CF4FD3}.Debug|Any CPU.Build.0 = Debug|Any CPU
5456
{66A030C3-69B8-4A84-9F4B-308D66CF4FD3}.Release|Any CPU.ActiveCfg = Release|Any CPU
5557
{66A030C3-69B8-4A84-9F4B-308D66CF4FD3}.Release|Any CPU.Build.0 = Release|Any CPU
58+
{7C23FCEB-BFC1-4720-87E1-004FA59A0FBC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
59+
{7C23FCEB-BFC1-4720-87E1-004FA59A0FBC}.Debug|Any CPU.Build.0 = Debug|Any CPU
60+
{7C23FCEB-BFC1-4720-87E1-004FA59A0FBC}.Release|Any CPU.ActiveCfg = Release|Any CPU
61+
{7C23FCEB-BFC1-4720-87E1-004FA59A0FBC}.Release|Any CPU.Build.0 = Release|Any CPU
5662
EndGlobalSection
5763
GlobalSection(SolutionProperties) = preSolution
5864
HideSolutionNode = FALSE
5965
EndGlobalSection
6066
GlobalSection(NestedProjects) = preSolution
6167
{A92C1287-BF2B-4FDC-8BBA-A17F410CD41F} = {25D84E98-E107-45C9-A0EC-0B25E24DA607}
6268
{66A030C3-69B8-4A84-9F4B-308D66CF4FD3} = {25D84E98-E107-45C9-A0EC-0B25E24DA607}
69+
{7C23FCEB-BFC1-4720-87E1-004FA59A0FBC} = {25D84E98-E107-45C9-A0EC-0B25E24DA607}
6370
EndGlobalSection
6471
GlobalSection(ExtensibilityGlobals) = postSolution
6572
SolutionGuid = {5EB08541-5168-443C-B524-A5CB7E7C613D}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Threading.Tasks;
5+
using Microsoft.AspNetCore.Mvc;
6+
using Numpy;
7+
using Python.Runtime;
8+
9+
namespace WebApiExample.Controllers
10+
{
11+
[Route("api/[controller]")]
12+
[ApiController]
13+
public class ValuesController : ControllerBase
14+
{
15+
// GET api/values
16+
[HttpGet]
17+
public ActionResult<string> Get()
18+
{
19+
using (Py.GIL()) {
20+
var array = new float[2, 2] {{DateTime.Now.Minute, DateTime.Now.Second}, {DateTime.Now.Millisecond, (float)Math.PI}};
21+
var ndArray = new NDarray(array);
22+
//return ndArray.ToString();
23+
return ndArray.repr;
24+
}
25+
}
26+
27+
// GET api/values/5
28+
[HttpGet("{id}")]
29+
public ActionResult<string> Get(int id)
30+
{
31+
using (Py.GIL())
32+
{
33+
var array = new float[2, 2] { { DateTime.Now.Minute, DateTime.Now.Second }, { DateTime.Now.Millisecond, (float)Math.PI } };
34+
var ndArray = new NDarray(array);
35+
//return ndArray.ToString();
36+
return ndArray.repr;
37+
}
38+
}
39+
40+
//// POST api/values
41+
//[HttpPost]
42+
//public void Post([FromBody] string value)
43+
//{
44+
//}
45+
46+
//// PUT api/values/5
47+
//[HttpPut("{id}")]
48+
//public void Put(int id, [FromBody] string value)
49+
//{
50+
//}
51+
52+
//// DELETE api/values/5
53+
//[HttpDelete("{id}")]
54+
//public void Delete(int id)
55+
//{
56+
//}
57+
}
58+
}

WebApiExample/Program.cs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.IO;
4+
using System.Linq;
5+
using System.Threading.Tasks;
6+
using Microsoft.AspNetCore;
7+
using Microsoft.AspNetCore.Hosting;
8+
using Microsoft.Extensions.Configuration;
9+
using Microsoft.Extensions.Logging;
10+
using Numpy;
11+
12+
namespace WebApiExample
13+
{
14+
public class Program
15+
{
16+
public static void Main(string[] args)
17+
{
18+
// this call initializes numpy. it is necessary to do that before PythonEngine.BeginAllowThreads()
19+
np.arange(1);
20+
Python.Runtime.PythonEngine.BeginAllowThreads(); // <--- this is very important for a web server since all requests are on different threads
21+
CreateWebHostBuilder(args).Build().Run();
22+
}
23+
24+
public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
25+
WebHost.CreateDefaultBuilder(args)
26+
.UseStartup<Startup>();
27+
}
28+
}

WebApiExample/Startup.cs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Threading.Tasks;
5+
using Microsoft.AspNetCore.Builder;
6+
using Microsoft.AspNetCore.Hosting;
7+
using Microsoft.AspNetCore.Mvc;
8+
using Microsoft.Extensions.Configuration;
9+
using Microsoft.Extensions.DependencyInjection;
10+
using Microsoft.Extensions.Logging;
11+
using Microsoft.Extensions.Options;
12+
13+
namespace WebApiExample
14+
{
15+
public class Startup
16+
{
17+
public Startup(IConfiguration configuration)
18+
{
19+
Configuration = configuration;
20+
}
21+
22+
public IConfiguration Configuration { get; }
23+
24+
// This method gets called by the runtime. Use this method to add services to the container.
25+
public void ConfigureServices(IServiceCollection services)
26+
{
27+
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
28+
}
29+
30+
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
31+
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
32+
{
33+
if (env.IsDevelopment())
34+
{
35+
app.UseDeveloperExceptionPage();
36+
}
37+
38+
app.UseMvc();
39+
}
40+
}
41+
}

WebApiExample/WebApiExample.csproj

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<Project Sdk="Microsoft.NET.Sdk.Web">
2+
3+
<PropertyGroup>
4+
<TargetFramework>netcoreapp2.2</TargetFramework>
5+
<AspNetCoreHostingModel>InProcess</AspNetCoreHostingModel>
6+
</PropertyGroup>
7+
8+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
9+
<PlatformTarget>x64</PlatformTarget>
10+
</PropertyGroup>
11+
12+
<ItemGroup>
13+
<PackageReference Include="Microsoft.AspNetCore.App" />
14+
<PackageReference Include="Microsoft.AspNetCore.Razor.Design" Version="2.2.0" PrivateAssets="All" />
15+
</ItemGroup>
16+
17+
<ItemGroup>
18+
<ProjectReference Include="..\src\Numpy.Bare\Numpy.Bare.csproj" />
19+
</ItemGroup>
20+
21+
</Project>
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"Logging": {
3+
"LogLevel": {
4+
"Default": "Debug",
5+
"System": "Information",
6+
"Microsoft": "Information"
7+
}
8+
}
9+
}

WebApiExample/appsettings.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"Logging": {
3+
"LogLevel": {
4+
"Default": "Warning"
5+
}
6+
},
7+
"AllowedHosts": "*"
8+
}

src/Numpy.Bare/Numpy.Bare.csproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@
1919
<AssemblyVersion>3.7.1.4</AssemblyVersion>
2020
</PropertyGroup>
2121

22+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
23+
<PlatformTarget>x64</PlatformTarget>
24+
</PropertyGroup>
25+
2226
<ItemGroup>
2327
<Compile Remove="Resources\**"></Compile>
2428
<EmbeddedResource Remove="Resources\**"></EmbeddedResource>

0 commit comments

Comments
 (0)