Commit b23bd596 authored by Oliver Meyer's avatar Oliver Meyer
Browse files

Merge branch 'copy' into 'master'

Copy

See merge request bzbmeyer/flask-learning-env!1
parents dc68b0a6 3a68da35
# configuration and readme
!.gitignore
!.gitpod.yml
!.gitpod.Dockerfile
!learn.json
!README.md
# exercises
!.learn/
!.learn/*
.learn/_app
.learn/.session
.learn/dist
.learn/app.tar.gz
.learn/config.json
# python compiled files
*.pyc
__pycache__/
.pytest_cache/
\ No newline at end of file
FROM gitpod/workspace-full:latest
RUN sudo apt update && sudo apt upgrade -y
RUN npm install -g learnpack && learnpack plugins:install learnpack-python
USER trainee
image:
file: .gitpod.Dockerfile
ports:
- port: 5000
onOpen: open-preview
tasks:
- command: learnpack start -g incremental
vscode:
extensions:
- "ms-python.python"
\ No newline at end of file
# 00 - Hello World with Flask
It's recommended to use the latest version of Python 3. Flask itselfs supports Python 3.5 and newer, Python 2.7, and PyPy.
To check the installed version of Python use the following command:
`python --version` or `python3 --version`
Dependencies are automatically installed when FLask will be installed. So the next step is to install Flask.
`pip install Flask`
After you successfully installed Python, the next step is to deploy your first Flask Application. Let's start with an Basic Application, which looks like this.
```python
from flask import Flask
app = Flask(__name__)
@app.route('/')
def hello_world():
return 'Hello, World!'
```
First, lets talk about the code.
1. First we imported the Flask class. An instance of this class will be our WSGI application.
2. Next we create an instance of this class.
3. We then use the route() decorator to tell Flask waht URL should trigger our function.
4. The function is given a name which is also used to generate URLs for that particular function, and returns the message we want to display in the user’s browser.
As you understand what we did we can safe the app as `app.py` for example. To run the application you can use either the `flask` command or the python's `-m` switch. But first you need to tell your terminal the application to work with by exporting the `FLASK_APP` environment variable:
```bash
$ export FLASK_APP=hello.py
$ flask run
* Running on http://127.0.0.1:5000/
```
\ No newline at end of file
# 01 - Routing
Modern web applications use meaningful URLs to help users. Users are more likely to like a page and come back if the page uses a meaningful URL they can remember and use to directly visit a page.
Use the **route()** decorator to bind a function to a URL.
```python
@app.route('/')
def index():
return 'Index Page'
@app.route('/hello')
def hello():
return 'Hello, World'
```
## Variable Rules
You can add variable sections to a URL by marking sections with `<variable_name>`. Your function then receives the `<variable_name>` as a keyword argument. Optionally, you can use a converter to specify the type of the argument like `<converter:variable_name>`.
```python
from markupsafe import escape
@app.route('/user/<username>')
def show_user_profile(username):
# show the user profile for that user
return 'User %s' % escape(username)
@app.route('/post/<int:post_id>')
def show_post(post_id):
# show the post with the given id, the id is an integer
return 'Post %d' % post_id
@app.route('/path/<path:subpath>')
def show_subpath(subpath):
# show the subpath after /path/
return 'Subpath %s' % escape(subpath)
```
\ No newline at end of file
https://flask.palletsprojects.com/en/1.1.x/quickstart/
\ No newline at end of file
https://runnable.com/docker/python/dockerize-your-flask-application
\ No newline at end of file
# Welcome to <%= it.title %>
You have initialized the exercises with grading=`incremental`; meaning that your students will complete one single tutorial with clear and autograded steps from beginning to end.
Type `$ learnpack start` in your terminal to start the exercises.
\ No newline at end of file
# Learning Env Flask
THIS IS UNDER CONSTRUCTIONS
# notes siehe learnpack.co
\ No newline at end of file
{
"port": 3000,
"address": "http://localhost",
"editor": {
"mode": null,
"version": null
},
"dirPath": "./.learn",
"configPath": "./learn.json",
"outputPath": "./.learn/dist",
"publicPath": "/preview",
"grading": "incremental",
"exercisesPath": "./",
"webpackTemplate": null,
"disableGrading": false,
"actions": [
"build",
"test",
"reset"
],
"entries": {
"html": "index.html",
"vanillajs": "index.js",
"react": "app.jsx",
"node": "app.js",
"python3": "app.py",
"java": "app.java"
},
"difficulty": "easy",
"duration": 2,
"description": "",
"title": "Flask Learning Environment",
"slug": "flask-learning-environment"
}
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment