Skip to content

Commit 7165810

Browse files
committed
Add Rubicon to Android
1 parent 6ab11db commit 7165810

File tree

4 files changed

+40
-0
lines changed

4 files changed

+40
-0
lines changed

apps/android_pythonnative_2/app/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ android {
2222
python {
2323
pip {
2424
install "matplotlib"
25+
install "rubicon-java"
2526
}
2627
}
2728
}

apps/android_pythonnative_2/app/src/main/java/com/pythonnative/pythonnative/ui/home/HomeFragment.kt

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,18 @@ package com.pythonnative.pythonnative.ui.home
22

33
import android.graphics.BitmapFactory
44
import android.os.Bundle
5+
import android.util.Log
56
import android.view.LayoutInflater
67
import android.view.View
78
import android.view.ViewGroup
9+
import android.widget.Button
810
import android.widget.TextView
911
import androidx.fragment.app.Fragment
1012
import androidx.lifecycle.ViewModelProvider
1113
import androidx.lifecycle.lifecycleScope
1214
import com.pythonnative.pythonnative.databinding.FragmentHomeBinding
1315
import com.chaquo.python.PyException
16+
import com.chaquo.python.PyObject
1417
import com.chaquo.python.Python
1518
import com.chaquo.python.android.AndroidPlatform
1619
import kotlinx.coroutines.Dispatchers
@@ -38,6 +41,16 @@ class HomeFragment : Fragment() {
3841
}
3942
val py = Python.getInstance()
4043
val plotModule = py.getModule("plot")
44+
// val buttonModule = py.getModule("create_button")
45+
46+
// val buttonModule: PyObject?
47+
// try {
48+
// buttonModule = py.getModule("create_button")
49+
// } catch (e: PyException) {
50+
// Log.e("Python Error", "Error importing Python module", e)
51+
// binding.textHome.text = e.message
52+
// return binding.root
53+
// }
4154

4255
// Variables to keep the user's input
4356
val xInput = "1 2 3 4 5"
@@ -54,7 +67,14 @@ class HomeFragment : Fragment() {
5467
val bitmap = BitmapFactory.decodeByteArray(bytes, 0, bytes.size)
5568
binding.imageHome.setImageBitmap(bitmap)
5669
}
70+
// val buttonId = buttonModule.callAttr(
71+
// "create_button",
72+
// requireActivity()
73+
// ).toJava(Int::class.java)
74+
// val button = requireActivity().findViewById<Button>(buttonId)
75+
// binding.root.addView(button)
5776
} catch (e: PyException) {
77+
Log.e("Python Error", "Error executing Python code", e)
5878
binding.textHome.text = e.message
5979
}
6080
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
from rubicon.java import JavaClass
2+
3+
Button = JavaClass("android/widget/Button")
4+
5+
6+
def create_button(context):
7+
button = Button(context)
8+
button.setId(hash(button))
9+
button.setText("Button created in Python")
10+
return button.getId()

requirements.txt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,34 +8,43 @@ certifi==2023.5.7
88
chardet==5.1.0
99
charset-normalizer==3.1.0
1010
click==8.1.3
11+
contourpy==1.0.7
1112
cookiecutter==2.1.1
13+
cycler==0.11.0
1214
Django==4.2.1
1315
dmgbuild==1.6.1
1416
ds-store==1.3.1
17+
fonttools==4.39.4
1518
gitdb==4.0.10
1619
GitPython==3.1.31
1720
idna==3.4
1821
iniconfig==2.0.0
1922
Jinja2==3.1.2
2023
jinja2-time==0.2.0
24+
kiwisolver==1.4.4
2125
mac-alias==2.2.2
2226
markdown-it-py==2.2.0
2327
MarkupSafe==2.1.2
28+
matplotlib==3.7.1
2429
mdurl==0.1.2
2530
mypy-extensions==1.0.0
31+
numpy==1.24.3
2632
packaging==23.1
2733
pathspec==0.11.1
34+
Pillow==9.5.0
2835
platformdirs==3.5.1
2936
pluggy==1.0.0
3037
psutil==5.9.5
3138
Pygments==2.15.1
39+
pyparsing==3.0.9
3240
pyproject_hooks==1.0.0
3341
pytest==7.3.1
3442
python-dateutil==2.8.2
3543
python-slugify==8.0.1
3644
PyYAML==6.0
3745
requests==2.31.0
3846
rich==13.3.5
47+
rubicon-java==0.2.6
3948
rubicon-objc==0.4.6
4049
six==1.16.0
4150
smmap==5.0.0

0 commit comments

Comments
 (0)