Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

244
Visualizações
Add Javascript into Python created html

Hi i have a python script that creates a single html file and i cannot figure out how to add javascript into it

import pathlib
import os
 
html = ""
 
for file in pathlib.Path(".").iterdir():
    if file.is_file():
        if str(file).endswith(".png"):
            html += f"<center><img src='{file}'/ height=100%></center><br>"
            
 
with open("index2.html", "w") as outputfile:
    outputfile.write(html)
 
os.startfile("index2.html")

So far ive tried

import pathlib
import os
 
html = ""
 
for file in pathlib.Path(".").iterdir():
    if file.is_file():
        if str(file).endswith(".png"):
            html += f"<center><img src='{file}'/ height=50%></center><br><script>var imagesrc = document.getElementById("image.png").src;"

 
with open("index2.html", "w") as outputfile:
    outputfile.write(html)
 
os.startfile("index2.html")

but that doesnt seem to work at all

Any help would be appreciated thanks

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

Lets say you have a script.js file in your directory:

# File.py
html += "<script type=\"text/javascript\" src=\"script.js\"></script>"

If you want it directly in the .html file, you can do this:

html += f"<script type=\"text/javascript\">{javascript_content}</script>"

 

about 4 years ago · Juan Pablo Isaza Relatório

0

That probably didn't worked because your " (quotation mark) is interrupting your string at "image.png", you could use a single apostrophe, or just escape it using backslash.

html += f"<center><img src='{file}'/ height=50%></center><br><script>var imagesrc = document.getElementById(\"image.png\").src;"
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda