multiple python script in single website without Django

To create a website with multiple Python scripts without using Django, you can use the Flask framework. Flask is a lightweight web framework that provides flexibility and simplicity for creating web applications. You can organize your project into multiple scripts by modularizing your code into blueprints.

Here’s a step-by-step guide to create a website with multiple Python scripts using Flask:

Step 1: Install Flask

First, install Flask using pip:

pip install Flask

Step 2: Create the Flask Application Structure

Create a directory structure for your Flask application:

mkdir my_flask_app
cd my_flask_app
mkdir templates static

Create the following files:

  • app.py: Main application file
  • config.py: Configuration file
  • blueprints.py: File to register blueprints
  • Create directories for your modules (e.g., blog and shop), each with its own views.py and __init__.py files.

Step 3: Main Application File (app.py)

Create the main application file app.py:

from flask import Flask
from blueprints import register_blueprints

app = Flask(__name__)
register_blueprints(app)

if __name__ == '__main__':
app.run(debug=True)

Step 4: Configuration File (config.py)

Create a configuration file config.py:

class Config:
DEBUG = True
SECRET_KEY = 'your_secret_key'

def init_app(app):
app.config.from_object(Config)

Step 5: Blueprints Registration (blueprints.py)

Create a file blueprints.py to register your blueprints:

from blog import blog_blueprint
from shop import shop_blueprint

def register_blueprints(app):
app.register_blueprint(blog_blueprint, url_prefix='/blog')
app.register_blueprint(shop_blueprint, url_prefix='/shop')

Step 6: Blog Module

Create a directory blog with __init__.py and views.py.

blog/__init__.py

from flask import Blueprint

blog_blueprint = Blueprint('blog', __name__)

from . import views

blog/views.py

from flask import render_template
from . import blog_blueprint

@blog_blueprint.route('/')
def home():
posts = [
{'title': 'First Post', 'content': 'Content of the first post.'},
{'title': 'Second Post', 'content': 'Content of the second post.'}
]
return render_template('blog/home.html', posts=posts)

Step 7: Shop Module

Create a directory shop with __init__.py and views.py.

shop/__init__.py

from flask import Blueprint

shop_blueprint = Blueprint('shop', __name__)

from . import views

shop/views.py

from flask import render_template
from . import shop_blueprint

@shop_blueprint.route('/')
def home():
products = [
{'name': 'Product 1', 'description': 'Description of product 1.', 'price': 10.0},
{'name': 'Product 2', 'description': 'Description of product 2.', 'price': 20.0}
]
return render_template('shop/home.html', products=products)

Step 8: Templates

Create the necessary templates.

templates/blog/home.html

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Blog Home</title>
</head>
<body>
<h1>Blog Posts</h1>
<ul>
{% for post in posts %}
<li>
<h2>{{ post.title }}</h2>
<p>{{ post.content }}</p>
</li>
{% endfor %}
</ul>
</body>
</html>

templates/shop/home.html

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Shop Home</title>
</head>
<body>
<h1>Products</h1>
<ul>
{% for product in products %}
<li>
<h2>{{ product.name }}</h2>
<p>{{ product.description }}</p>
<p>Price: ${{ product.price }}</p>
</li>
{% endfor %}
</ul>
</body>
</html>

Step 9: Run the Application

Run your Flask application:

python app.py

Navigate to http://127.0.0.1:5000/blog/ to see the blog section and http://127.0.0.1:5000/shop/ to see the shop section.

Additional Features

You can further enhance your Flask application by adding:

  1. User Authentication: Use Flask extensions like Flask-Login for managing user authentication.
  2. Database Integration: Use Flask-SQLAlchemy for database integration.
  3. Forms and Validations: Use Flask-WTF for handling forms and validations.
  4. API Development: Use Flask-RESTful for developing RESTful APIs.
  5. Testing: Write unit tests using Flask-Testing or pytest-flask.
  6. Deployment: Deploy your application to production using services like Heroku, AWS, or DigitalOcean.

By organizing your Flask project with blueprints, you can keep your code modular and maintainable, making it easier to manage as your project grows.


Discover more from Soa Technology | Aditya Website Development Designing Company

Subscribe to get the latest posts sent to your email.



Leave a Reply

Discover more from Soa Technology | Aditya Website Development Designing Company

Subscribe now to keep reading and get access to the full archive.

Continue reading