(Feb-01-2020, 01:31 PM)oldbean Wrote: When using for example JavaScript or PHP you can embed the scripts within the HTML. Where would you place a python script prior to execution?
The closet way to the workflow in PHP(with HTML/CSS/JavaScript) is to use eg
Flask.
To give a demo that i have posted before,install is
pip install Flask
.
This is all you need for local development(Flask has build in web server),forget about WAMP,LAMP or XAMPP as you may have used in PHP.
Setup folder/files:
my_app
|-- app.py
templates\
|-- index.html
|-- about.html
static\
css\
|-- style.css
js\
img\
app.py:
from flask import Flask, render_template, jsonify, request
app = Flask(__name__)
@app.route('/')
def index():
return render_template("index.html")
@app.route('/about', methods=['GET'])
def about():
return render_template("about.html")
if __name__ == '__main__':
app.run(debug=True)
index.html:
<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>Some title</title>
<link rel=stylesheet type=text/css href="{{ url_for('static', filename='css/style.css') }}" />
</head>
<body>
<div id="start">
<h1>Start page</h1>
<ul>
<li class="page_1"><a href="/about">About</a></li>
</ul>
</div>
</body>
</html>
about.html:
<div class="foo">
<h1>The About Page</h1>
<a href="https://python-forum.io/">Python forum</a>
</div>
style.css:
body {
background: #67B26F;
background: -webkit-linear-gradient(to right, #4ca2cd, #67B26F);
background: linear-gradient(to right, #4ca2cd, #67B26F);
font-size: 100%;
padding:0;
margin:0;
}
html {
height: 100%
}
Now from command line in folder where
app.py
is.
Run
python app.py
.
Then it look like this:
E:\all_flask\my_app
λ python app.py
* Serving Flask app "app" (lazy loading)
* Environment: production
WARNING: Do not use the development server in a production environment.
Use a production WSGI server instead.
* Debug mode: on
* Restarting with stat
* Debugger is active!
* Debugger PIN: 334-187-997
* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
See address copy that to browser
http://127.0.0.1:5000/
.
Then should show the page and with a link to about page.
So can to all development local to a finish web-app.
Only if want to share with world look for a Python friendly host eg DigitalOcean.
Now leave the local web-server and switch to a production ready web-server like
Gunicorn.
How To Serve Flask Applications with Gunicorn and Nginx on Ubuntu 18.04