How to run a python script from html - html

I have been trying to run a python script (rainbow.py) through a HTML button. I have no idea how to do this and all the answers I have found make no sense to me. All I want is a simple explanation of how to run a python script through regular HTML script.
I am trying to do this on a Raspberry Pi.

You can make it through Flask, a Python Web Microframework:
HTML (index.html):
Your button
Python (app.py):
from flask import Flask, render_template
app = Flask(__name__)
#app.route('/')
def index():
# render your html template
return render_template('index.html')
#app.route('/something')
def do_something():
# when the button was clicked,
# the code below will be execute.
print 'do something here'
...
if __name__ == '__main__':
app.run()
Start the server:$ python app.py
Then go to http://127.0.0.1:5000.
File structure:
template\
-index.html
app.py

Related

Can we run appium test scripts through flask server on locally connected device

I am trying to run my appium scripts on button click in a html template, but unfortunately I have searched everywhere but still I found no solution.
My html template is inside a templates folder in the flask project directory and I am taking path as an input and want to pass that string to my Appium script and as soon as I click on the Submit button on my template it should launch the test script on my locally connected phone taking the path as a parameter.
Any kind of help will be appreciated.
Regards
I tried adding functions of my test script in the flask route but I have multiple functions in my python script and for every function I would have to create multiple routes. I was expecting to run the python script all at once on the default flask route ('/').
Following is my code for flask_server.py file as per now I am just getting the parameter in it and showing it in the next route but instead I want to pass my appium script here and run it on device on submit.
from flask import Flask
from flask import render_template
from flask import request
from Instagram.instagram_android_automation import
InstagramAndroidAutomation
from flask import Flask, redirect, url_for, request
app = Flask(__name__)
#app.route('/dashboard/<name>')
def dashboard(name):
return 'welcome %s' % name
#app.route('/login',methods = ['POST', 'GET'])
def login():
if request.method == 'POST':
user = request.form['name']
return redirect(url_for('dashboard',name = user))
else:
user = request.args.get('name')
return render_template('login.html')
if __name__ == '__main__':
app.run(debug = True)

how to automatically open a website when launching the dash?

I am using plotly-dash with jupyter dash. I am wondering if I can automatically open a website if Jupyter dash is run and launch the dashboad after app.run_server(mode='external',debug=True,port=8050).
The reason is I have to log in a website to connect to the data for the dashboard.
Thanks
Dash runs on Flask in the background, so I found a similar question for Flask which can be adapted for dash similarly (credit to both responders to that question at the time of writing this answer).
Here is an example on how you can adapt it for Dash:
import os
from threading import Timer
import webbrowser
import dash
from dash import html
from dash import dcc
app = dash.Dash(__name__)
app.layout = html.Div(
[
dcc.DatePickerRange(id='date-range')
]
)
def open_browser():
if not os.environ.get("WERKZEUG_RUN_MAIN"):
webbrowser.open_new('http://127.0.0.1:1222/')
if __name__ == "__main__":
Timer(1, open_browser).start()
app.run_server(debug=True, port=1222)

variable routes values get in static files path

I was working on web app using cs50 ide but when i changed to my machine everything worked except for variable-routes their values get in the start of static files
I am using Flask and VS Code and it was working well on the ide and I made sure to copy everything and made sure I copied correctly
This is one of my routes but this route adds the word "posts" to the static file path
#app.route("/posts/<post_id>", methods=["GET", "POST"])
#login_required
def open_post(post_id):
if request.method == "GET":
.....

Start a script to Raspberry Pi without refresh the page

I have a streaming server for my Raspberry Pi. I want to be able to control it with servos (pan and tilt) from the website. Therefore I want to start a python script, that starts when a botton is pressed at the website, without refreshing the page. Is there a way to do that? I'm using flask.
You would want to setup an endpoint in your flask app like:
from flask import Flask
app = Flask(__name__)
#app.route("/")
def indexpage():
# Serve your index web page here
return app.send_static_file('index.html')
#app.route("/runscript")
def runscript():
# Do whatever you want here
run_my_script()
app.run()
Then in your webpage have a form which sends a GET request to your app at the runscript endpoint.
<form action="/runscript" method="get">
<input type="submit" value="Submit">
</form>

Django compressor minifying the css but not gziping [duplicate]

I'm running a django 1.4.1 app.
I didn't realize that just including django.contrib.staticfiles into INSTALLED_APPS in your settings is enough to get static files served while settings.DEBUG is True, i.e., you don't have to manually add anything to your urls file.
I also noticed that this bypasses the django middleware. Does anyone know how or why this happens?
I just created a blank new project, my views.py:
from django.http import HttpResponse
def index(request):
html = '<html><body>Logo: <img src="/static/logo.gif"></body></html>'
return HttpResponse(html)
My urls.py:
from django.conf.urls import patterns, include, url
urlpatterns = patterns('',
url(r'^$', 'testapp.views.index', name='home'),
)
My settings.py has specified a directory to look for static files, and it also has this added:
MIDDLEWARE_CLASSES = (
'testapp.middleware.TestMiddleware',
...
)
Using this middleware:
from __future__ import print_function
class TestMiddleware(object):
def process_request(self, request):
print("[REQUEST]", request.path)
And when I make a request, this gets printed out:
[REQUEST] /
[18/Jan/2013 15:30:27] "GET / HTTP/1.1" 200 60
[18/Jan/2013 15:30:27] "GET /static/logo.gif HTTP/1.1" 200 2190
[REQUEST] /favicon.ico
Is it something to do with how the test server starts up?
I just figured this out after posting…
If you're using django-admin.py runserver or python manage.py runserver, then it does some extra magic to add a staticfiles handler that your regular middleware can't touch.
You can disable this by running django-admin.py runserver --nostatic — see the django docs
And when you do --nostatic it will fall back to the urls in your app, such as if you include staticfiles_urls() directly with:
urlpatterns += staticfiles_urlpatterns()
then your middleware will run for those urls (and of course all your others).
Found this issue when trying to modify request.path with middleware.
Discovered urls resolve against request.path_info not request.path