Spaces:
Sleeping
Sleeping
Upload 4 files
Browse files- app.py +14 -0
- index.html +34 -0
- requirements.txt +3 -0
- sample_rust/src/lib.rs +12 -0
app.py
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from flask import Flask, request
|
| 2 |
+
from flask_cors import CORS
|
| 3 |
+
import sample_rust
|
| 4 |
+
import os
|
| 5 |
+
os.environ["FLASK_ENV"] = "development"
|
| 6 |
+
|
| 7 |
+
app = Flask(_name_)
|
| 8 |
+
CORS(app) # Allow CORS for all routes
|
| 9 |
+
|
| 10 |
+
@app.route("/process", methods=["POST"])
|
| 11 |
+
def process():
|
| 12 |
+
name = request.form.get("name", "World")
|
| 13 |
+
result = sample_rust.hello(name)
|
| 14 |
+
return result
|
index.html
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
<!DOCTYPE html>
|
| 2 |
+
<html>
|
| 3 |
+
<head>
|
| 4 |
+
<title>Rust Hello App</title>
|
| 5 |
+
</head>
|
| 6 |
+
<body>
|
| 7 |
+
<h2>Rust + Flask + JS Fetch (Local HTML + HF Backend)</h2>
|
| 8 |
+
<label for="name">Enter your name:</label>
|
| 9 |
+
<input type="text" id="name" name="name">
|
| 10 |
+
<button onclick="sendData()">Say Hello</button>
|
| 11 |
+
|
| 12 |
+
<p id="result" style="font-weight:bold;"></p>
|
| 13 |
+
|
| 14 |
+
<script>
|
| 15 |
+
function sendData() {
|
| 16 |
+
const name = document.getElementById('name').value;
|
| 17 |
+
fetch("https://<your-hf-space>.hf.space/process", {
|
| 18 |
+
method: "POST",
|
| 19 |
+
headers: {
|
| 20 |
+
"Content-Type": "application/x-www-form-urlencoded",
|
| 21 |
+
},
|
| 22 |
+
body: name=${encodeURIComponent(name)}
|
| 23 |
+
})
|
| 24 |
+
.then(response => response.text())
|
| 25 |
+
.then(data => {
|
| 26 |
+
document.getElementById("result").innerText = data;
|
| 27 |
+
})
|
| 28 |
+
.catch(error => {
|
| 29 |
+
document.getElementById("result").innerText = "Error: " + error;
|
| 30 |
+
});
|
| 31 |
+
}
|
| 32 |
+
</script>
|
| 33 |
+
</body>
|
| 34 |
+
</html>
|
requirements.txt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
flask
|
| 2 |
+
flask-cors
|
| 3 |
+
maturin
|
sample_rust/src/lib.rs
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
use pyo3::prelude::*;
|
| 2 |
+
|
| 3 |
+
#[pyfunction]
|
| 4 |
+
fn hello(name: &str) -> PyResult<String> {
|
| 5 |
+
Ok(format!("Hello, {}! Welcome from Rust 🦀", name))
|
| 6 |
+
}
|
| 7 |
+
|
| 8 |
+
#[pymodule]
|
| 9 |
+
fn sample_rust(_py: Python, m: &PyModule) -> PyResult<()> {
|
| 10 |
+
m.add_function(wrap_pyfunction!(hello, m)?)?;
|
| 11 |
+
Ok(())
|
| 12 |
+
}
|