0% found this document useful (0 votes)
21 views

Build A Simple Web Application With Flashk

Uploaded by

newel31882
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views

Build A Simple Web Application With Flashk

Uploaded by

newel31882
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

Building a Simple Web Application with Flask

What is Flask?
Flask is a lightweight, easy-to-use micro-framework for building web applications
in Python. It provides essential tools to get a basic web application up and
running.

Setting Up Flask:
1. Install Flask:
```bash
pip install flask
```

2. Create a Basic Flask App:


```python
from flask import Flask

app = Flask(__name__)

@app.route('/')
def home():
return "Hello, World!"

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

3. Run the app:


```bash
python app.py
```

This will start a local server at `http://127.0.0.1:5000/`.

Routing and Templates:


- Flask uses decorators to map URLs to functions.
- HTML templates are stored in the `templates` directory, and you can use the
`render_template` function to render them.
Example:
```python
from flask import render_template

@app.route('/hello/<name>')
def hello(name):
return render_template('hello.html', name=name)
```

4. HTML Template (`templates/hello.html`):


```html
<html>
<body>
<h1>Hello, {{ name }}!</h1>
</body>
</html>
```

Connecting to a Database:
- Use SQLite for simplicity.
- Install SQLite:
```bash
pip install flask-sqlalchemy
```
- Example for setting up an SQLite database:
```python
from flask import Flask
from flask_sqlalchemy import SQLAlchemy

app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///site.db'
db = SQLAlchemy(app)
```

Deploying to Heroku:
1. Create a `Procfile`:
```
web: python app.py
```
2. Initialize a Git repository and push to GitHub.
3. Deploy using the Heroku CLI.

Conclusion:
Flask is a simple yet powerful framework for building web applications. By
following these steps, you can quickly create a basic web app and deploy it online.

You might also like