Skip to content

Commit 18b0865

Browse files
committed
Add ui_layout.py
1 parent cb568cf commit 18b0865

File tree

3 files changed

+83
-20
lines changed

3 files changed

+83
-20
lines changed

apps/android_pythonnative_3/app/src/main/java/com/pythonnative/pythonnative/MainActivity.kt

Lines changed: 58 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,47 +4,85 @@ import android.graphics.BitmapFactory
44
import androidx.appcompat.app.AppCompatActivity
55
import android.os.Bundle
66
import android.util.Log
7+
import android.widget.Button
78
import android.widget.ImageView
9+
import android.widget.TextView
10+
import android.graphics.Color
11+
import androidx.constraintlayout.widget.ConstraintLayout
812
import com.chaquo.python.PyException
913
import com.chaquo.python.Python
1014
import com.chaquo.python.android.AndroidPlatform
1115
import kotlinx.coroutines.CoroutineScope
1216
import kotlinx.coroutines.Dispatchers
1317
import kotlinx.coroutines.launch
1418
import kotlinx.coroutines.withContext
19+
import org.json.JSONObject
1520

1621
class MainActivity : AppCompatActivity() {
1722
override fun onCreate(savedInstanceState: Bundle?) {
1823
super.onCreate(savedInstanceState)
1924
setContentView(R.layout.activity_main)
2025

21-
val imageView = findViewById<ImageView>(R.id.image_home)
22-
2326
// Initialize Chaquopy
2427
if (!Python.isStarted()) {
2528
Python.start(AndroidPlatform(this))
2629
}
30+
2731
val py = Python.getInstance()
28-
val plotModule = py.getModule("plot")
29-
// val createButtonModule = py.getModule("create_button")
30-
val xInput = "1 2 3 4 5"
31-
val yInput = "1 4 9 16 25"
32-
CoroutineScope(Dispatchers.Main).launch {
33-
try {
34-
val bytes = plotModule.callAttr(
35-
"plot",
36-
xInput,
37-
yInput
38-
).toJava(ByteArray::class.java)
39-
withContext(Dispatchers.IO) {
40-
val bitmap = BitmapFactory.decodeByteArray(bytes, 0, bytes.size)
41-
withContext(Dispatchers.Main) {
42-
imageView.setImageBitmap(bitmap)
32+
33+
// Generate UI from Python
34+
val uiLayoutModule = py.getModule("ui_layout")
35+
val layoutJson = uiLayoutModule.callAttr("generate_layout").toString()
36+
val layout = JSONObject(layoutJson)
37+
val widgets = layout.getJSONArray("widgets")
38+
39+
for (i in 0 until widgets.length()) {
40+
val widget = widgets.getJSONObject(i)
41+
when (widget.getString("type")) {
42+
"Button" -> {
43+
val button = Button(this)
44+
button.text = widget.getJSONObject("properties").getString("text")
45+
button.setTextColor(Color.parseColor(widget.getJSONObject("properties").getString("textColor")))
46+
button.setBackgroundColor(Color.parseColor(widget.getJSONObject("properties").getString("backgroundColor")))
47+
48+
if (widget.has("eventHandlers") && widget.getJSONObject("eventHandlers").has("onClick")) {
49+
val onClickFunctionName = widget.getJSONObject("eventHandlers").getString("onClick")
50+
val onClickFunction = py.getModule("ui_layout").get(onClickFunctionName)
51+
52+
button.setOnClickListener {
53+
onClickFunction?.call()
54+
}
4355
}
56+
57+
// Add button to your layout here
58+
val layoutMain = findViewById<ConstraintLayout>(R.id.layout_main)
59+
layoutMain.addView(button)
4460
}
45-
} catch (e: PyException) {
46-
Log.e("Python Error", "Error executing Python code", e)
61+
// Handle other widget types...
4762
}
4863
}
64+
65+
// Existing code for displaying plot
66+
// val imageView = findViewById<ImageView>(R.id.image_home)
67+
// val plotModule = py.getModule("plot")
68+
// val xInput = "1 2 3 4 5"
69+
// val yInput = "1 4 9 16 25"
70+
// CoroutineScope(Dispatchers.Main).launch {
71+
// try {
72+
// val bytes = plotModule.callAttr(
73+
// "plot",
74+
// xInput,
75+
// yInput
76+
// ).toJava(ByteArray::class.java)
77+
// withContext(Dispatchers.IO) {
78+
// val bitmap = BitmapFactory.decodeByteArray(bytes, 0, bytes.size)
79+
// withContext(Dispatchers.Main) {
80+
// imageView.setImageBitmap(bitmap)
81+
// }
82+
// }
83+
// } catch (e: PyException) {
84+
// Log.e("Python Error", "Error executing Python code", e)
85+
// }
86+
// }
4987
}
50-
}
88+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import json
2+
3+
4+
def on_button_click():
5+
print("Button clicked!")
6+
7+
8+
def generate_layout():
9+
layout = {
10+
"widgets": [
11+
{
12+
"type": "Button",
13+
"properties": {
14+
"text": "Click me!",
15+
"textColor": "#FFFFFF",
16+
"backgroundColor": "#DB4437",
17+
},
18+
"eventHandlers": {
19+
"onClick": "on_button_click",
20+
}
21+
},
22+
]
23+
}
24+
return json.dumps(layout)

apps/android_pythonnative_3/app/src/main/res/layout/activity_main.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
33
xmlns:app="http://schemas.android.com/apk/res-auto"
44
xmlns:tools="http://schemas.android.com/tools"
5+
android:id="@+id/layout_main"
56
android:layout_width="match_parent"
67
android:layout_height="match_parent"
78
tools:context=".MainActivity">

0 commit comments

Comments
 (0)