Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
fzumstein committed Nov 14, 2024
1 parent a200be0 commit cf97899
Show file tree
Hide file tree
Showing 8 changed files with 17 additions and 40 deletions.
3 changes: 3 additions & 0 deletions app/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,9 @@ async def add_security_headers(request, call_next):
# https://owasp.org/www-project-secure-headers/index.html#configuration-proposal
# https://owasp.org/www-project-secure-headers/ci/headers_add.json
response = await call_next(request)
if not settings.add_security_headers and settings.environment == "dev":
# Prevent caching in dev even if security headers are switched off
response.headers["Cache-Control"] = "no-store, max-age=0"
if settings.add_security_headers:
data = read_security_headers()

Expand Down
6 changes: 3 additions & 3 deletions app/static/js/core/custom-functions-code.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ let runtime;
let contentLanguage;
let socket = null;

let pyscriptDone = new Promise((resolve) => {
let pyscriptAllDone = new Promise((resolve) => {
window.addEventListener(
"py:done",
"py:all-done",
() => {
resolve(true);
},
Expand Down Expand Up @@ -267,7 +267,7 @@ async function makeServerCall(body) {
}

async function makeWasmCall(body) {
await pyscriptDone;
await pyscriptAllDone;
let r = await window.custom_functions_call(JSON.stringify(body));
return r.toJs();
}
Expand Down
2 changes: 1 addition & 1 deletion app/static/js/core/sheet-buttons.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ async function registerCellToButton(hyperlinkCellRef, scriptName, xwConfig) {
: "";
if (config.onWasm) {
let body = await xlwings.getBookData(xwConfig);
await pyscriptDone;
await pyscriptAllDone;
let r = await window.custom_scripts_call(body, scriptName);
r = JSON.parse(r);
// let actions = r.toJs();
Expand Down
6 changes: 3 additions & 3 deletions app/static/js/core/xlwingsjs/xlwings.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ const xlwings = {
};
globalThis.xlwings = xlwings;

let pyscriptDone = new Promise((resolve) => {
let pyscriptAllDone = new Promise((resolve) => {
// Duplicated in custom-functions-code.js
window.addEventListener(
"py:ready",
"py:all-done",
() => {
resolve(true);
},
Expand Down Expand Up @@ -60,7 +60,7 @@ export function init() {
// TODO: this is duplicated in sheet-buttons.js
if (config.onWasm) {
let body = await xlwings.getBookData(xwConfig);
await pyscriptDone;
await pyscriptAllDone;
let r = await window.custom_scripts_call(
body,
element.getAttribute("xw-click"),
Expand Down
Binary file added app/static/xlwings-0.0.0-py3-none-any.whl
Binary file not shown.
1 change: 1 addition & 0 deletions app/wasm/custom_scripts/examples.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ async def test(book: xw.Book):
sheet1 = book.sheets[0]
print(sheet1["A1:A2"].value)
book.sheets[0]["A3"].value = random.random()
book.sheets[0]["A4"].value = xw.__version__

# fig = plt.figure()
# plt.plot([1, 2, 3])
Expand Down
36 changes: 5 additions & 31 deletions app/wasm/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,22 @@
- Check out https://docs.pyscript.net/2024.5.2/user-guide/workers/
TODO:
- make current_user optional
- look into toJS vs json (func vs script)
- .env file
- when changing body of custom function, the function seem to call a cached version?
- run 10000 custom functions
- run 10000 custom functions (also on Windows)
- remove CDNs
- automatic pyscript.json config?
- ObjectConverter
- static page CLI build command (code, meta, custom-scripts-sheet-buttons, etc.)
- ObjectConverter
"""

import json
import os
from typing import Optional

os.environ["XLWINGS_LICENSE_KEY"] = "noncommercial"
import custom_functions
import custom_scripts
import xlwings as xw # noqa: E402
from pydantic import BaseModel
from pyscript import window # type: ignore # noqa: E402
from xlwings.server import (
custom_functions_call as xlwings_custom_functions_call,
Expand All @@ -33,10 +29,11 @@


async def custom_functions_call(data):
current_user = User(id="n/a", name="Anonymous")
# current_user = User(id="n/a", name="Anonymous")
data = json.loads(data)
rv = xlwings_custom_functions_call(
data, module=custom_functions, current_user=current_user
data,
module=custom_functions,
)
return rv

Expand All @@ -46,35 +43,12 @@ async def custom_functions_call(data):

async def custom_scripts_call(data, script_name):
book = xw.Book(json=json.loads(data))
current_user = User(id="n/a", name="Anonymous")
book = await xlwings_custom_scripts_call(
module=custom_scripts,
script_name=script_name,
current_user=current_user,
typehint_to_value={xw.Book: book},
)
return json.dumps(book.json())


window.custom_scripts_call = custom_scripts_call


class User(BaseModel):
id: str
name: str
email: Optional[str] = None
domain: Optional[str] = None
roles: Optional[list[str]] = []

async def has_required_roles(self, required_roles: Optional[list[str]] = None):
if required_roles:
if set(required_roles).issubset(self.roles):
return True
else:
return False
else:
return True

async def is_authorized(self):
"""Here, you can implement a custom authorization logic"""
return True
3 changes: 1 addition & 2 deletions app/wasm/pyscript.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
{
"packages": [
"xlwings",
"/static/xlwings-0.0.0-py3-none-any.whl",
"sqlite3",
"pydantic",
"numpy",
"pandas"
],
Expand Down

0 comments on commit cf97899

Please sign in to comment.