mutiline python script in html (pyscript)

2024/9/20 20:36:09

I've tried to use pyscript in html but i can only get it to work in one line of code can somebody help me get it to work for the following code?

def vpn(website):from selenium import webdriverfrom selenium.webdriver.chrome.options import Optionschrome_options = Options()chrome_options.add_argument('--no-sandbox')chrome_options.add_argument('--disable-dev-shm-usage')driver = webdriver.Chrome(options=chrome_options)driver.get(website)vpn("chrome://newtab/")

The only code that I would even assume that would work is:

<!DOCTYPE html>
<html><pyscript>def vpn(website):from selenium import webdriverfrom selenium.webdriver.chrome.options import Optionschrome_options = Options()chrome_options.add_argument('--no-sandbox')chrome_options.add_argument('--disable-dev-shm-usage')driver = webdriver.Chrome(options=chrome_options)driver.get(website)vpn("chrome://newtab/")</pyscript>
</html>

When I run said code, I get this error message:

def vpn(website): from selenium import webdriver from selenium.webdriver.chrome.options import Options chrome_options = Options() chrome_options.add_argument('--no-sandbox') chrome_options.add_argument('--disable-dev-shm-usage') driver = webdriver.Chrome(options=chrome_options) driver.get(website) vpn("chrome://newtab/")

Does anyone know any other way to do this?

Answer

Maybe, just maybe, there is an indentation problem. Pyscript code written inside html file will error if tab character is used for indentation even just once instead of spaces and the other way arround. It is hard to notice it as the code with tabs and with 4 spaces looks the same. Example:

<py-script>
import pandas as pd
data = {'A': [1, 2, 3, 4, 5], 'B': ['A', 'B', 'C', 'D', 'E']}
if data:
____df = pd.DataFrame(data)
____print('Print from index.html')
____print(df)
____pyscript.write('out', df)
</py-script>

This code above works ok because there are 4 spaces ( _ _ _ _ ) used for indentation. The code below ...

<py-script>
import pandas as pd
data = {'A': [1, 2, 3, 4, 5], 'B': ['A', 'B', 'C', 'D', 'E']}
if data:
____df = pd.DataFrame(data)
--->print('Print from index.html')
____print(df)
____pyscript.write('out', df)
</py-script>

... will cause an error because the command print('Print from index.html') is indented using tab (--->). In my case that tab indentation is set to 4 chars but just the fact that the tab is used instead of 4 spaces forces an error.
I don't write the pyscript code within the html file at all. Instead, I do it in the file and put it in the html as src attribute of pyscript tag.
The same code in that case is:
main.py file:

import pandas as pd
data = {'A': [1, 2, 3, 4, 5], 'B': ['A', 'B', 'C', 'D', 'E']}
if data:df = pd.DataFrame(data)print('Print from main.py')print(df)pyscript.write('out', df)

index.html file

...
<py-script id="outerPyMain" src="./main.py"></py-script>
...

In addition the "no tab char" rule is implemented within py-env tag too. To conclude, the indentation for pyscript tag should be always spaces or always tabs - no mixing allowed. For py-env tag it is just spaces.
Don't know if this is your case, but anyway...
Regards...

https://en.xdnf.cn/q/119284.html

Related Q&A

How to write an output of a command to stdout and a file in Python3?

I have a Windows command which I want to write to stdout and to a file. For now, I only have 0 string writen in my file:#!/usr/bin/env python3 #! -*- coding:utf-8 -*-import subprocesswith open(auto_cha…

Mongodb adding a new field in an existing document, with specific position

I am facing this issue where I need to insert a new field in an existing document at a specific position. Sample document: { "name": "user", "age" : "21", "…

how to check every 3 x 3 box in sudoku?

I am trying to build a sudoku solver without much googling around. Right now, I am working on a function to test whether the board is valid, which I will use later in a loop. This is what the function …

multiple model accuracy json result format using python

I am building a multiple model and i am getting results with 7 models accuracy, i need those results with a proper json format.My multiple model building code will be like thisseed = 7"prepare mod…

Calculate Time Difference based on Conditionals

I have a dataframe that looks something like this (actual dataframe is millions of rows):ID Category Site Task Completed Access Completed1 A X 1/2/22 12:00:00AM 1/1/22 12:00:00 AM1 A Y 1/3/22 12:00:00A…

Cannot open jpg images with PIL or open()

I am testing to save ImageField in Django, but for some reason all the *.jpg files Ive tried dont work while the one png I had works. Using django shell in WSL VCode terminal. python 3.7 django 3.0 pil…

how to delete tensorflow model before retraining

I cant retrain my image classifier with new images, I get the following error:AssertionError: Export directory already exists. Please specify a different export directory: /tmp/saved_models/1/How do I …

Use beautifulsoup to scrape a table within a webpage?

I am scraping a county website that posts emergency calls and their locations. I have found success webscraping basic elements, but am having trouble scraping the rows of the table. (Here is an example…

Encrypt folder or zip file using python

So I am trying to encrypt a directory using python and Im not sure what the best way to do that is. I am easily able to turn the folder into a zip file, but from there I have tried looking up how to en…

Use Python Element Tree to parse xml in ASCII text file [closed]

Closed. This question needs debugging details. It is not currently accepting answers.Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to repro…