Last active
December 11, 2024 09:41
-
-
Save shuantsu/8a85568c689e35914c1cb5ae4f6430b7 to your computer and use it in GitHub Desktop.
chaquopy snippets
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
xmlns:app="http://schemas.android.com/apk/res-auto" | |
xmlns:tools="http://schemas.android.com/tools" | |
android:id="@+id/main" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
tools:context=".MainActivity"> | |
<WebView | |
android:id="@+id/webview" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
tools:layout_editor_absoluteX="1dp" | |
tools:layout_editor_absoluteY="1dp" /> | |
</androidx.constraintlayout.widget.ConstraintLayout> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<manifest xmlns:android="http://schemas.android.com/apk/res/android" | |
xmlns:tools="http://schemas.android.com/tools"> | |
<uses-permission android:name="android.permission.INTERNET"/> | |
<application | |
android:allowBackup="true" | |
android:usesCleartextTraffic="true" | |
android:dataExtractionRules="@xml/data_extraction_rules" | |
android:fullBackupContent="@xml/backup_rules" | |
android:icon="@mipmap/ic_launcher" | |
android:label="@string/app_name" | |
android:roundIcon="@mipmap/ic_launcher_round" | |
android:supportsRtl="true" | |
android:theme="@style/Theme.MyApchaquopy3000" | |
tools:targetApi="31"> | |
<activity | |
android:name=".MainActivity" | |
android:exported="true"> | |
<intent-filter> | |
<action android:name="android.intent.action.MAIN" /> | |
<category android:name="android.intent.category.LAUNCHER" /> | |
</intent-filter> | |
</activity> | |
</application> | |
</manifest> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import threading | |
from flask import Flask, render_template, request | |
import sys | |
app = Flask(__name__) | |
@app.route('/') | |
def index(): | |
return render_template("index.html") | |
@app.route('/noma', methods=['GET']) | |
def noma(): | |
args = request.args | |
name = args.get('name') | |
return render_template("name_is.html", name=name) | |
def start_flask_server(): | |
for PORT in range(0, 100000): | |
try: | |
app.run(port=PORT,debug=False) | |
break | |
except Exception as e: | |
pass | |
def run_server_once(): | |
server_thread = threading.Thread(target=start_flask_server) | |
server_thread.daemon = True # Allow the app to exit even if the thread is running | |
server_thread.start() | |
if __name__ == "__main__": | |
if sys.argv[1] == '--run': | |
start_flask_server() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Top-level build file where you can add configuration options common to all sub-projects/modules. | |
plugins { | |
alias(libs.plugins.android.application) apply false | |
alias(libs.plugins.jetbrains.kotlin.android) apply false | |
id("com.chaquo.python") version "16.0.0" apply false | |
} | |
// FOLLOWING CODE IS SAME FILENAME BUT DIFFERENT FILE! | |
// FOLLOWING CODE IS SAME FILENAME BUT DIFFERENT FILE! | |
// FOLLOWING CODE IS SAME FILENAME BUT DIFFERENT FILE! | |
// FOLLOWING CODE IS SAME FILENAME BUT DIFFERENT FILE! | |
// FOLLOWING CODE IS SAME FILENAME BUT DIFFERENT FILE! | |
// FOLLOWING CODE IS SAME FILENAME BUT DIFFERENT FILE! | |
// Module app | |
plugins { | |
alias(libs.plugins.android.application) | |
alias(libs.plugins.jetbrains.kotlin.android) | |
id("com.chaquo.python") | |
} | |
android { | |
namespace = "com.filipeteixeira.myapchaquopy3000" | |
compileSdk = 35 | |
defaultConfig { | |
applicationId = "com.filipeteixeira.myapchaquopy3000" | |
minSdk = 24 | |
targetSdk = 35 | |
versionCode = 1 | |
versionName = "1.0" | |
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner" | |
ndk { | |
// On Apple silicon, you can omit x86_64. | |
abiFilters += listOf("arm64-v8a", "x86_64", "x86") | |
} | |
} | |
buildTypes { | |
release { | |
isMinifyEnabled = false | |
proguardFiles( | |
getDefaultProguardFile("proguard-android-optimize.txt"), | |
"proguard-rules.pro" | |
) | |
} | |
} | |
compileOptions { | |
sourceCompatibility = JavaVersion.VERSION_1_8 | |
targetCompatibility = JavaVersion.VERSION_1_8 | |
} | |
kotlinOptions { | |
jvmTarget = "1.8" | |
} | |
} | |
chaquopy { | |
defaultConfig { | |
pip { | |
install("requests") | |
install("beautifulsoup4") | |
install("flask") | |
} | |
buildPython("C:/Python312/python.exe") | |
} | |
} | |
dependencies { | |
implementation(libs.androidx.core.ktx) | |
implementation(libs.androidx.appcompat) | |
implementation(libs.material) | |
implementation(libs.androidx.activity) | |
implementation(libs.androidx.constraintlayout) | |
testImplementation(libs.junit) | |
androidTestImplementation(libs.androidx.junit) | |
androidTestImplementation(libs.androidx.espresso.core) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.filipeteixeira.myapchaquopy3000 | |
import android.annotation.SuppressLint | |
import android.os.Bundle | |
import android.view.View | |
import android.webkit.WebResourceRequest | |
import android.webkit.WebView | |
import android.webkit.WebViewClient | |
import android.widget.TextView | |
import androidx.activity.enableEdgeToEdge | |
import androidx.appcompat.app.AppCompatActivity | |
import androidx.core.view.ViewCompat | |
import androidx.core.view.WindowInsetsCompat | |
import com.chaquo.python.Python | |
import com.chaquo.python.android.AndroidPlatform | |
class MainActivity : AppCompatActivity() { | |
@SuppressLint("MissingInflatedId", "SetJavaScriptEnabled") | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
enableEdgeToEdge() | |
setContentView(R.layout.activity_main) | |
ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v, insets -> | |
val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars()) | |
v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom) | |
insets | |
} | |
// "context" must be an Activity, Service or Application object from your app. | |
if (! Python.isStarted()) { | |
Python.start(AndroidPlatform(this)); | |
} | |
val py = Python.getInstance() | |
val mod = py.getModule("app") | |
mod.callAttr("run_server_once") | |
val webView = findViewById<WebView>(R.id.webview) | |
webView.settings.javaScriptEnabled = true | |
class MyWebViewClient : WebViewClient() { | |
override fun shouldOverrideUrlLoading(view: WebView?, request: WebResourceRequest?): Boolean { | |
// Do not open new browser to load new link, load them in this webview | |
return false | |
} | |
} | |
webView.webViewClient = MyWebViewClient() | |
webView.loadUrl("http://127.0.0.1:1234") | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment