To develop a website from scratch using Python, the developer has to be familiar with various web frameworks such as Django, Grok, WebPy, TurboGears, WebApp2, and Pyramid, and Flask. If you happen to know these frameworks, here are the steps to create a Python website:
Step1: Create an empty file and save it with a .pyextension before running it.
Here is the code from Flask framework:
from flask importFlask
app=Flask(__name__)
@app.route(‘/’)
def home():
return“Website content goes here.
” if __name__ ==’__main__’:
app.run(debug=True)
This code will transform into a web application prototype created using python.
Step 2: Running the Code
● Visit localhost:5000 to experience the first look of your website (it will appear plain as it doesn’t have any content).
● Then you will employ Python functions to return an output to the source URL.
Step 3: Render Templates
● Create a home.html file inside the ‘Templates’ folder. (Remember to create this folder at the same directory level as that of your Python file)
● Here is the code for rendering templates.
from flask importFlask,
render_template app=Flask(__name__)
@app.route(‘/’)
def home():
return render_template(“home.html”)
if __name__ ==’__main__’:
app.run(debug=True)
For functional guidance here are some useful tips:
● It would be better if you run your website in a virtual environment.
● To create a virtual environment, you can use the virtualenv library:
pip install virtualenvpython-m venvfoldername
Step1: Create an empty file and save it with a .pyextension before running it.
Here is the code from Flask framework:
from flask importFlask
app=Flask(__name__)
@app.route(‘/’)
def home():
return“Website content goes here.
” if __name__ ==’__main__’:
app.run(debug=True)
This code will transform into a web application prototype created using python.
Step 2: Running the Code
● Visit localhost:5000 to experience the first look of your website (it will appear plain as it doesn’t have any content).
● Then you will employ Python functions to return an output to the source URL.
Step 3: Render Templates
● Create a home.html file inside the ‘Templates’ folder. (Remember to create this folder at the same directory level as that of your Python file)
● Here is the code for rendering templates.
from flask importFlask,
render_template app=Flask(__name__)
@app.route(‘/’)
def home():
return render_template(“home.html”)
if __name__ ==’__main__’:
app.run(debug=True)
For functional guidance here are some useful tips:
● It would be better if you run your website in a virtual environment.
● To create a virtual environment, you can use the virtualenv library:
pip install virtualenvpython-m venvfoldername