Skip to main content

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.

  1. 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.

Metadata

Release files for localstorage-flask 2.0.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for localstorage-flask 2.0.1
File Size Uploaded
localstorage_flask-2.0.1.tar.gz 4.2 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for localstorage-flask 2.0.1
File Interpreter ABI Platform
localstorage_flask-2.0.1-py3-none-any.whl Python 3 none any Details

Total release size: 8.2 kB

Release files / localstorage_flask-2.0.1.tar.gz

Download URL localstorage_flask-2.0.1.tar.gz
Size 4.2 kB
Tags Source
SHA-256 checksum
How to use checksums
0cf0f112aed6c22fae104e6492cc02cbca4e2f5c4810ec567721fe9c32fb85df
BLAKE2b-256 checksum
How to use checksums
79aad06e4aee6afb16c3d1b21785afbdb236d6efafd67a874ae912da60944927
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.0.0 CPython/3.12.3

Release files / localstorage_flask-2.0.1-py3-none-any.whl

Download URL localstorage_flask-2.0.1-py3-none-any.whl
Size 4.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
12efd1bbe6e5239443e327756c406751a733723c649dd47f02914e81b56a0a2f
BLAKE2b-256 checksum
How to use checksums
cf2e555d8f4295ab002759aa78cfafe8d62ede203477635126b3d5590df63899
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.0.0 CPython/3.12.3

Release history Release notifications | RSS feed

This release

2.0.1 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page