I am trying to run python script through html button but I am getting the following error. I don't know why. kindly help to get rid off it. The error is:
module 'sys' has no attribute 'execute'
views.py:
from subprocess import run, PIPE
from django.shortcuts import render
import requests
import sys
def external(request):
out = run([sys.execute,'/home/abc/Documents/test.py'], shell=False, stdout=PIPE)
print(out)
return render(request, 'home.html', {{'data1': out}})
home.html:
<html>
<head>
<title> RUN PYTHON SCRIPT </title>
</head>
<body>
<form action='{% url "external" %}' method="post">
{% csrf_token %}
<input type="submit" name="btn" value="start analyse" id="toggle1" onclick="location.href='{% url 'external' %}'" />
</form>
</body>
</html>
urls.py:
path('external/', views.external, name="external"),
Even the button doesn't execute the test.py python script and gives the following error:
module 'sys' has no object 'execute' in views.py
kindly help me to run script through html button
Try to use subprocess library like
import subprocess
subprocess.run(["ls", "-l", "/dev/null"], capture_output=True)
Related
I am using eel to communicate with python. I'm working in dir C:\Users\Desktop\Eel where I have app.py and inside the UI folder I have index.html, myjava.js, style.css, images but nothing called eel.js. I said this because in docs it says to include script called /eel.js.
index.html
<head>
<script type="text/javascript" language="JavaScript" src="./myjava.js"></script>
<script type="text/javascript" src="/eel.js"></script>
<link rel="stylesheet" href="style.css">
</head>
<body>
<div class="container" onclick="runeel()">
</body>
my Javascript is:
function runeel(){
eel.runpy()
}
app.py
import eel
eel.init('UI')
eel.start('index.html', size=(900, 550))
#eel.expose
def runpy():
....code which creates an excel file in desktop...
When I run the py file, the index.html loads up and then when I click the div I get into the function and but it throws the error:
myjava.js:2 Uncaught TypeError: eel.runpy is not a function
What am I missing?
It seems that the init statement is written first.
What about writing the #eel.expose statement right after the import statement?
I'm not good at English, so I'm worried that it will make sense.
But I would be happy if I could help.
In my case, I had to start eel after exposing the function via the decorator:
import eel
eel.init('UI')
#eel.expose
def runpy():
....code which creates an excel file in desktop...
eel.start('index.html', size=(900, 550))
i had the same problem until I removed the /eel.js from
<script type="text/javascript" src="/eel.js"></script>
now it's look like
<script type="text/javascript" src="eel.js"></script>
I'm having an issue when using Flask and it's templates when importing Scripts and Stylesheets. I searched everywhere how to do it, and none of them solved my problem.
This is my project's structure:
https://i.gyazo.com/f746c58499cbbafa11b023eecb316b15.png
This is how I defined Flask's configuration:
app = Flask(__name__,
template_folder="../web/templates",
static_folder="../web/static",
static_url_path=''
)
And this is how i'm importing the scripts :
<script src={{ url_for('static', filename='/js/home.js') }}></script>
And this is the msg I get :
Falló la carga de con fuente “http://localhost:5000/js/home.js”.
(Basically says that it failed loading that script)
I already read the Quickstart guide from Flask.
I tried creating a "static" folder in the project's root and not setting anything on the config. Tried to change the static folder everywhere on my project.
Can anyone help me solve this? I'm new to Flask and Jinja, but have been searching the whole day and can't make it work.
NOTE : The path for the templates work, my templates work fine, except for .js and .css imports.
you need to remove starting '/' from <script src={{ url_for('static',filename='/js/home.js') }}.
so change it to <script src={{ url_for('static',filename='js/home.js') }}.
your project structure should look like this:
MyFlaskApp/templates/home.html
MyFlaskApp/static/js/home.js
MyFlaskApp/app.py
app.py
from flask import Flask, render_template
app = Flask(__name__)
#app.route("/")
def home():
return render_template("home.html")
if __name__ == "__main__":
app.run()
home.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<script src={{ url_for('static',filename='js/home.js') }}> </script>
</body>
</html>
home.js
alert("hello");
I have the following django template and I have defined a button in it so that it calls the javascript function which I added to the page like below. I have the below issues:
1) If I bring the script tags to the header section nothings shows up.
2) If press the button I get the undefined error message. (Uncaught ReferenceError: postData).
What am I mssing here?
Thank you very much,
Mike
newproduct.html
{% load staticfiles %}
<!DOCTYPE html>
<html>
<head>
<title>New Product</title>
</head>
<body>
<p>Hellow</p>
Back
<h2>Please enter product details:</h2>
<button onclick="postData();">Send Post</button>
<form action="http://127.0.0.1:8000/newproduct/" method="post">
<p>{{ message }}</p>
{% csrf_token %}
{{ form.as_p }}
<input type="submit"/>
</form>
<script src="bower_components/jquery/src/jquery.js" type="text/javascript"/>
<script src="scripts/main.js" />
</body>
</html>
main.js
function postData ()
{
$.post('/newproduct', {name:'New Product JS', price:555, });
};
You've hard-coded the reference to postData in your HTML, but you've used it before you've defined it, since the JS isn't loaded until the end of the page.
You could fix this by moving the script to the top of the HTML, but it is better not to use JS directly in HTML like this. Rather, your script itself should use the jQuery dom-ready functionality to bind itself to the button event.
html:
<h2>Please enter product details:</h2>
<button id="my_button">Send Post</button>
main.js
$(function() {
$('#my_button').click(function() {
$.post('/newproduct', {name:'New Product JS', price:555, });
});
});
Maybe you're getting trouble handling the path url, instead use the full path, try something like:
{% static ‘path/to/file’ %}
In your settings.py
STATICFILES_DIRS = (os.path.join(BASE_DIR, "static"),)
Remember that your static file dir is setted in the same level of your project.
I'm using the built-in flask server and I want to show an animated pie chart, like here:
http://codepen.io/tpalmer/pen/jqlFG
or
http://jsfiddle.net/thmain/xL48ru9k/1/
For simplicity, I use the latter one.
The python flask server code is:
from flask import Flask, render_template, request, flash
from forms import ContactForm
app = Flask(__name__)
app.secret_key = 'blah'
#app.route('/', methods = ['GET', 'POST'])
def test():
return render_template('test.html')
if __name__ == '__main__':
app.run(debug = True)
I did copy the javascript code provided on the webpage to the file static/script2.js
and the css code to static/css/style_d3.css
My HTML code is
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<link rel="stylesheet" href="//style_d3.css" type="text/css">
</head>
<body>
<p>test</p>
<div class="animated-ring">
<svg></svg>
</div>
<script src="http://d3js.org/d3.v3.min.js"></script>
<script src="//script2.js"></script>
</body>
</html>
I run the webpage through the flask server. But I get a webpage that just says "test", there's no chart.
What do I do wrong?
I'd really appreciate if someone could help me.
You only render the test.html at "/" path, the script2.js needs to be rendered too at "/script2.js".
Try this one liner at your project's root directory to host all files:
python -m SimpleHTTPServer
I think you need to set to a path your js file. In your app dir create a static folder then put your script2.js file and when you call this file at html use flask url_for() function
src="{{url_for('static', filename='script2.js'}}"
When the gray div is clicked I want the website to be opened in a new tab, but here this javascript code isn't working. Why? How I can edit it?
views.py:
from django.shortcuts import render
from django.http import HttpResponse
def test(request):
return render(request, 'test.html')
test.html:
<html>
<head>
<script src="jquery.min.js"></script>
<script>
$(document).ready(function(){
$("#gray").click(function(){
window.open("http://www.w3schools.com");
});
});
</script>
</head>
<body>
<div id='gray' style="width:100px;height:100px;background:gray;"></div>
</body>
urls.py:
from django.conf.urls import include, url
from secondv.views import test
urlpatterns = [
url(r'^test', test),
]
The jquery.min.js file is in the template directory, the same directory as test.html file.
Template directory is only for templates. All static content such as js, images should be placed to static directory. It's well described in docs
https://docs.djangoproject.com/en/1.9/intro/tutorial06/
https://docs.djangoproject.com/en/1.9/howto/static-files/deployment/ - for production