Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is it possible to develop an Android .NET MAUI app using CSnake? #322

Closed
Camee99 opened this issue Jan 7, 2025 · 5 comments · Fixed by #345
Closed

Is it possible to develop an Android .NET MAUI app using CSnake? #322

Camee99 opened this issue Jan 7, 2025 · 5 comments · Fixed by #345

Comments

@Camee99
Copy link

Camee99 commented Jan 7, 2025

It seems that the main issue is that CSnake needs python to be installed on the device to work. Is it possible to bypass this point? Do you have any suggestion on how to face it?

@tonybaloney
Copy link
Owner

I'm working on a solution to this at the moment.

I'll add another automated locator which pulls in managed versions via UV/Astral's service.

https://docs.astral.sh/uv/concepts/python-versions/#managed-python-distributions

At the moment, the only solution for this is to use the nuget package, but that only bundles Windows x86_64 binaries.

@tonybaloney
Copy link
Owner

That solution is released in the newest version, see release notes and please let me know if this works for your use case.

Use .FromRedistributable() on the environment builder and it will download Python for you.

@Camee99
Copy link
Author

Camee99 commented Jan 13, 2025

I have added the .FromRedistributable() locator to the environment builder and no error is reported, but when the program reaches the line "var env = app.Services.GetRequiredService();" it stops after about 6 minutes throwing the exception "System.TimeoutException: 'Python installation timed out.'". I'm reporting exactly what it happens as I'm not an expert and I can't judge if it is a problem related to my program or to the .FromRedistributable() Locator. I can say that the same program for Windows machine using python from the FromNuGet or the FromFolder locators works.
Thanks for the support!
Here is the code of my MauiProgram.cs file:

using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Hosting;
using CSnakes.Runtime;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using System.IO;
using System.Text.Json;
using Microsoft.Extensions.Configuration;
using System.Diagnostics;

namespace PythonApp
{
    public static class MauiProgram
    {
        public static MauiApp CreateMauiApp()
        {
            var builder = MauiApp.CreateBuilder();
            builder
                .UseMauiApp<App>()
                .ConfigureFonts(fonts =>
                {
                    fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular");
                    fonts.AddFont("OpenSans-Semibold.ttf", "OpenSansSemibold");
                });

            var home = Path.Join(Environment.CurrentDirectory, "..", "..", "..", "..", "PythonDependencyProg");
            var venv = Path.Join(home, ".venv");
            builder.Services.WithPython()
                .WithHome(home)
                .WithVirtualEnvironment(venv)
                .FromRedistributable()
                .WithPipInstaller();


            builder.Services.AddSingleton<MainPage>();

            var app = builder.Build();

            var env = app.Services.GetRequiredService<IPythonEnvironment>();

            RunDemo(env);

            return app;
        }

        public static void RunDemo(IPythonEnvironment env)
        {
            var demo=env.Demo();
            string output = demo.HelloWorld("cami");
        }
    }
}

@tonybaloney
Copy link
Owner

I'm experimenting with MAUI desktop apps in #341 and will look at mobile after that.

@atifaziz
Copy link
Contributor

atifaziz commented Jan 23, 2025

it stops after about 6 minutes throwing the exception "System.TimeoutException: 'Python installation timed out.'"

This sounds like a timeout waiting for the mutex and another installation to complete. Is it possible that you had an instance of the app running blocked and you launched another instance that then timed out waiting for the first one to complete? My gut feeling says that the first instance remained blocked waiting for the download to complete on the UI thread, but which is busy serving the download in the first place; therefore deadlock! I just opened PR #345 that should hopefully address this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants