Python: Difference between revisions

From Wiki RB4
 
Line 21: Line 21:
  u'<STRING>' // unicode
  u'<STRING>' // unicode


====Outuput====
====Output====
  print(<STRING>)
  print(<STRING>)



Latest revision as of 12:37, 13 November 2025

Resources[edit]

Language[edit]

Python, named after the British comedy group Monty Python, is a high-level, interpreted, interactive, and object-oriented programming language.

Comment[edit]

# ... till end of line

__name__[edit]

The __name__ variable (two underscores before and after) is a special Python variable. It gets its value depending on how we execute the containing script. When you run the script, the __name__ variable equals '__main__'. When you import the script, it will contain the name of the imported script w/o extension (see C:\Uwes\python\UdacityFullWebDeveloper\testName\).

Strings[edit]

Operators[edit]

+ for concatenation

String Modifiers[edit]

f'<STRING>' // format modifier
u'<STRING>' // unicode

Output[edit]

print(<STRING>)

Pip[edit]

pip is the standard package manager for Python.

Virtual Environments[edit]

With Venv it is easy to create virtual environments, obviously using the python version and binaries, which were used to initialize the environment (global environment), as a initial setup. A virtual environment is a subfolder in a project that contains a copy of a specific interpreter. When you activate the virtual environment, any packages you install are installed only in that environment's subfolder. When you then run a Python program within that environment, you know that it's running against only those specific packages.

Frameworks[edit]

Flask[edit]

Flask is a micro web framework written in Python. Flask is using a template engine called Jinja.

The standard to run a Flask application is:

$env:FLASK_APP = "flaskr" // folder to look for the init file
/flaskr/__init__.py // with method def create_app()

or

$env:FLASK_APP = "xyz.py" // file with the flask app

and then

flask run --reload 

To enable to run a flask application as a 'normal' python app with

python <FILE>

add to end of file

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

and to enable debugging and reloading set in Powershell (or call app.run(debug=True))

$env:FLASK_ENV = "development"

which starts a http server on port 5000.

By default html files are located in a subfolder called 'templates'. They can be rendered by calling render_template().

Decorators[edit]

@app.route(<PATH_STRING>[, methods=['<METHOD_LIST']]) 
// PATH_STRING can have <<CONVERTER>:<VARIABLE_NAME>>
// request.method contains the Http method

Flask SQL Alchemy[edit]

  1. to set a foreign key constraint in database

Flask Migration and Flask Script[edit]

Flask-Migrate uses the Alembic library, a database migration tool, underneath. It auto-detects changes from the old to the new version of the SQLAlchemy models. It creates migration scripts and enables fine-grain control to change existing tables.

<PROJECT_DIR> flask db init // creates initial migrations folder structure, 
flask db migrate // check the current model in python and the former model in DB and creates versions in migrations folder, it will not create the database but only the schmema
flask db upgrade // checks against a database table 'alembic_version' (if there otherwise it will be created) and runs unapplied migrations up to the latest version
flask db downgrade
Installation[edit]
Configuration[edit]
SQLALCHEMY_ECHO = True // prints SQL statements to console

Operation[edit]

Comannd Line[edit]

python -V // prints version
python
>>> <COMMAND>

Installation[edit]