A Flask extension for local storage
Project description
LocalStorageFlask LocalStorageFlask là một thư viện dành cho Flask giúp bạn dễ dàng thao tác với localStorage trong trình duyệt người dùng từ phía server. Thư viện này hỗ trợ các lệnh để lưu trữ, truy xuất và xóa dữ liệu trong localStorage.
Cài đặt Để cài đặt LocalStorageFlask, bạn có thể cài đặt thư viện từ PyPI bằng lệnh sau:
pip install localstorage_flask
Cách sử dụng Mã Python (Flask) Đầu tiên, bạn cần khởi tạo một ứng dụng Flask và sử dụng thư viện LocalStorageFlask.
from flask import Flask, render_template
from localstorage_flask import LocalStorageFlask
app = Flask(__name__)
# Khởi tạo thư viện LocalStorageFlask
localstorage = LocalStorageFlask(app)
@app.route("/")
def index():
# Lưu thông tin vào localStorage
localstorage.setItem("username", "currentUser")
localstorage.setItem("theme", "dark")
# Lấy dữ liệu từ localStorage
username = localstorage.getItem("username", "username")
theme = localstorage.getItem("theme", "theme")
# Trả về trang HTML với mã JavaScript cần thiết
return render_template("index.html")
if __name__ == "__main__":
app.run(debug=True)
Mã HTML Template Trong mã HTML này, bạn có thể tương tác với localStorage và hiển thị dữ liệu từ localStorage sau khi lấy từ Flask.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>LocalStorage Example</title>
</head>
<body>
<h1>Welcome to LocalStorageFlask</h1>
<p>Open the browser's console to see localStorage operations in action.</p>
<h2>LocalStorage Data:</h2>
<p id="username">Username: </p>
<p id="theme">Theme: </p>
<button id="fetchDataBtn">Fetch LocalStorage Data</button>
<script>
document.getElementById("fetchDataBtn").addEventListener("click", function() {
// Hiển thị giá trị từ localStorage vào các phần tử trên HTML
document.getElementById("username").innerText = "Username: " + username;
document.getElementById("theme").innerText = "Theme: " + theme;
});
</script>
</body>
</html>
Các lệnh để tương tác với localStorage Dưới đây là các lệnh mà bạn có thể sử dụng để tương tác với localStorage thông qua thư viện LocalStorageFlask.
- Lưu trữ dữ liệu vào localStorage Sử dụng lệnh sau để lưu trữ dữ liệu vào localStorage:
localstorage.setItem("key", "value")
key: Tên của mục bạn muốn lưu. value: Giá trị bạn muốn lưu trữ. 3. Lấy dữ liệu từ localStorage Để lấy dữ liệu từ localStorage, sử dụng lệnh sau:
localstorage.getItem("key", "variable_name")
key: Tên của mục mà bạn muốn lấy dữ liệu. variable_name: Tên biến JavaScript sẽ chứa giá trị của mục đã lấy. 3. Xóa mục khỏi localStorage Để xóa một mục khỏi localStorage, sử dụng lệnh sau:
localstorage.removeItem("key")
key: Tên của mục bạn muốn xóa. 4. Xóa toàn bộ dữ liệu trong localStorage Sử dụng lệnh sau để xóa toàn bộ dữ liệu trong localStorage:
localstorage.clear()
Kết luận LocalStorageFlask giúp bạn dễ dàng thao tác với localStorage trong các ứng dụng Flask. Các lệnh trên cho phép bạn lưu trữ, lấy và xóa dữ liệu từ localStorage mà không cần phải phụ thuộc vào cơ sở dữ liệu.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file localstorage_flask-2.0.1.tar.gz.
File metadata
- Download URL: localstorage_flask-2.0.1.tar.gz
- Upload date:
- Size: 4.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.0.0 CPython/3.12.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0cf0f112aed6c22fae104e6492cc02cbca4e2f5c4810ec567721fe9c32fb85df
|
|
| MD5 |
ae2accdb6aabe7aa3b47e90cc6f7d831
|
|
| BLAKE2b-256 |
79aad06e4aee6afb16c3d1b21785afbdb236d6efafd67a874ae912da60944927
|
File details
Details for the file localstorage_flask-2.0.1-py3-none-any.whl.
File metadata
- Download URL: localstorage_flask-2.0.1-py3-none-any.whl
- Upload date:
- Size: 4.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.0.0 CPython/3.12.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
12efd1bbe6e5239443e327756c406751a733723c649dd47f02914e81b56a0a2f
|
|
| MD5 |
10665e397bdb3bbc3233ce3a7839068c
|
|
| BLAKE2b-256 |
cf2e555d8f4295ab002759aa78cfafe8d62ede203477635126b3d5590df63899
|