Skip to content

Writing an Android and iOS application using Xamarin.Forms

kikocorreoso edited this page Jun 19, 2020 · 7 revisions

With Brython you can write applications in Python that run in modern browsers, including most of smartphones browsers. But you can also write native Android and iOS applications : this page explains how to do it, taking the example of a basic "Hello world" application using Xamarin.Forms

1. Installation Xamarin.Forms and development environment

The first step is to install the latest version of Visual Studio (https://docs.microsoft.com/en-us/xamarin/get-started/installation/?pivots=windows) and if you prefer as me JetBrain IDE-s then install Rider

2. Main View of Application

Xamarin.Forms is cross-platform framework written in C# that allow to write once and run everywhere UI Application ( on most of the platforms ;) )

The Main Page of entire application is set in App.cs of BrythonMobile project

using System;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;

[assembly: XamlCompilation(XamlCompilationOptions.Compile)]

namespace BrythonMobile
{
    public partial class App : Application
    {
        public App()
        {
            InitializeComponent();

            MainPage = new LocalHtmlBaseUrl {Title = "BaseUrl" };
        }

        protected override void OnStart()
        {
            // Handle when your app starts
        }

        protected override void OnSleep()
        {
            // Handle when your app sleeps
        }

        protected override void OnResume()
        {
            // Handle when your app resumes
        }
    }
}

3. Write the Brython application

In this example, we only use brython.js and index.html holds a basic "Hello" application:

<!DOCTYPE html>
<html>
    <head>
        <title>Hello world</title>
        <meta charset="utf-8">
        <script src="brython.js"></script>
    
    </head>
    
    <body onload="brython(1)">
    
        <script type="text/python">
        from browser import document, alert
        
        def echo(ev):
            alert("Hello {} !".format(document["zone"].value))
        
        document["test"].bind("click", echo)
        </script>
        <p>Your name is : <input id="zone" autocomplete="off">
        <button id="test">click !</button>
    </body>

</html>

Test project you can find by the following link

You can download the test project by the following link from GitHub:

BrythonMobile Test Project