Can I run a Python interpreter within a Flutter app?

3,379 views
Skip to first unread message

anar...@gmail.com

unread,
Feb 3, 2019, 7:01:37 PM2/3/19
to Flutter Dev

I'm new to Flutter and trying it for the first time (I'm not an experienced mobile developer either) and was wondering how to go about running a Python interpreter within my app.

What I'd actually like to create is an app with Python katas or challenges so that when the user solves a challenge it will tell them if it passed some tests (I assume I'd write those as unit tests).

Not sure where to start with all of that, though... I'm not even sure Flutter is the best choice to build that kind of app. So any guidance on how to start building it would be very welcome.

Thanks!

Ian B

unread,
Feb 4, 2019, 4:19:57 AM2/4/19
to anar...@gmail.com, Flutter Dev
If you want to write an app for mobile, I feel like Python is probably not what you want. You could write it in Dart for Flutter, or JavaScript for React or something like that. 

If you want to learn Flutter specifically, then learn Dart at the same time really is the way to go. It's not too tricky to get your head around the basics, and lots of resources.

--
You received this message because you are subscribed to the Google Groups "Flutter Dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to flutter-dev...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

anar...@gmail.com

unread,
Feb 4, 2019, 7:23:34 AM2/4/19
to Flutter Dev
No, actually, I'm not trying to code a mobile app with Python. What I want to do is a coding challenges app (like what you see on hackerrank, leetcode, etc, but on mobile), where you get some programming katas and get an editor where you can attempt to solve them, then the app runs a couple tests and it tells you if your code is ok or if it didn't pass some of those tests. And I need those coding challenges to be Python. So I need to run a Python interpreter at some point, to test the user's code.
I'm new to mobile programming (but not new to programming), so I thought Flutter would be a nice way to go, but I'm not even sure how to approach this or if it's even possible.

Ian B

unread,
Feb 4, 2019, 7:36:32 AM2/4/19
to anar...@gmail.com, Flutter Dev
I guess there's a couple of approaches...running Python on the mobile device (eg https://medium.com/@umerfarooq_26378/tools-to-run-python-on-android-9060663972b4 )...or passing the script to a backend server, running it, and returning the results.

However, I'm still a bit uncertain why anyone would want to do a coding challenge on a mobile (maybe if it was multiple choice questions or something, but actually coding on a mobile is pretty horrible experience). Maybe that's just me though, and I'm a bit old school :).


anar...@gmail.com

unread,
Feb 4, 2019, 8:19:57 AM2/4/19
to Flutter Dev
I know it'd be much better on desktop, but this is an educational app that would be used by teens, and they just adore their phones :P
If possible, I'd like to avoid using a backend server, so I'll take a look at that link to run Python on the device. Thanks!

Stefan Matthias Aust

unread,
Feb 4, 2019, 5:38:49 PM2/4/19
to Flutter Dev
Disclaimer: This isn't a serious answer, but having said that, there's always the option to create your own Python interpreter in Dart for embedding :)

I played around with Python ~10 years ago and created interpreters (and bytecode compilers) for subsets of Python. Inspired by this posting, I took the last two hours to translate some of that code to Dart. It is just enough to run the factorial function. That is, it is completely impractical in its current state :-) Nevertheless, if somebody is interested, here's the code: https://github.com/sma/smythondart 

On a more serious note: I'm more familiar with iOS than Android. Because iOS has its roots in C and Objective-C, I think, it should be possible to use the original CPython interpreter as a library and first create an Objective-C wrapper and then a native Flutter plugin for passing source code to that interpreter. The interpreter needs probably access to some system resources based on what it shall do. Creating Python C-bindings might be somewhat tedious. I'm not sure whether this would be worth the effort.

A scripting language which ought to be easier to embed is Lua.

It's also my guess that writing source code that depends on correct indentation is more difficult on a small device without a dedicated text editor or at least a normal multiline TextField without special keys for special symbols. Luas more Pascal-like syntax is easier to write, I think.

Stefan
Reply all
Reply to author
Forward
0 new messages