Pipenv

Install pipenv

### Fresh install
pip3 install pipenv

### Install with requirements.txt
pipenv install -r /my/path/../requirements.txt

### Activate virtual environment
pipenv shell
 or
pipenv run python3 __init__.py

### Deactivate virtual environment 
exit
 or
deactivate

### Install on my virtual environment
(myFlaskApp) pipenv install requests
(myFlaskApp) pipenv install Flask
(myFlaskApp) pipenv install flask_sqlalchemy
(myFlaskApp) pipenv install flask_wtf
(myFlaskApp) pipenv install flask-bcrypt
(myFlaskApp) pipenv install flask-login
(myFlaskApp) pipenv install Pillow

### Uninstall
pipenv uninstall requests

### Remove virtual environment
pipenv --rm

### Check my path to virtual environment
pipenv --venv

### Check for vulnerability 
pipenv check

### Check for dependencies
pipenv graph

Testing venv

>>> import sys
>>> sys.path.append("..")
>>> from flaskblog import db

### Write to db
>>> db.create_all()
>>> from flaskblog.models import User, Post
>>> user_1 = User(username='username1', email='username1@random.com', password='password')
>>> db.session.add(user_1)
>>> db.session.commit()

### Query all users
>>> User.query.all()
>>> User.query.first()
>>> User.query.filter_by(username='username1').all()
>>> User.query.filter_by(email = 'xyz@gmail.com').all()

### Post
>>> post_1 = Post(title='First Post Title', content='First Post Contents xyz', user_id=user.id)
>>> db.session.add(post_1)
>>> db.session.commit()

### Drop database
>>> db.drop_all()

### test Bcrypt
>>> from flask_bcrypt import Bcrypt
>>> bcrypt = Bcrypt()
>>> bcrypt.generate_password_hash('VeryStrongPassword')

### get hashed password in str
>>> bcrypt.generate_password_hash('VeryStrongPassword').decode('utf-8')
>>> hashed_pw = bcrypt.generate_password_hash('testing').decode('utf-8')

### Check hashed string against password
>>> bcrypt.check_password_hash(hashed_pw, 'testing')

### Test reading user from DB
>>> from flaskblog import db
>>> from flaskblog.models import User
>>> user = User.query.first()
>>> user

### View all properties and methods of the specified object
>>> dir(posts)

### Pagination
>>> posts = Post.query.paginate()
>>> posts.page
>>> posts.per_page
>>> posts.total
>>> posts.items
>>> posts = Post.query.paginate(per_page=5)
>>> posts = Post.query.paginate(page=2)

### Looping Pagination
>>> for post in posts.items:
...    print(post)

### Page in Pagination
>>> posts = Post.query.paginate(per_page=5, page=2)

### Pagination variable 
>>> for page in posts.iter_pages():
...    print(page)

### iter_pages
>>> for page in posts.iter_pages(left_edge=1, right_edge=1, left_current=1, right_current=2): 
... print(page)

itsdangerous token

>>> from itsdangerous import TimedJSONWebSignatureSerializer as Serializer
>>> s = Serializer('YOUR_CONFIG_KEY', 30)
>>> token = s.dumps('Your sensitive data').decode('utf-8')
>>> token
>>> s.loads(token)

Leave a Reply

Your email address will not be published. Required fields are marked *

Scroll to top