Trending December 2023 # How To Perform Migrate In Flask With Examples? # Suggested January 2024 # Top 20 Popular

You are reading the article How To Perform Migrate In Flask With Examples? updated in December 2023 on the website Kientrucdochoi.com. We hope that the information we have shared is helpful to you. If you find the content interesting and meaningful, please share it with your friends and continue to follow and support us for the latest updates. Suggested January 2024 How To Perform Migrate In Flask With Examples?

Definition on Flask Migrate

Flask migrate is defined as an extension that is used in the Flask application for handling database migrations for SQLAlchemy using Alembic. This module enables developers to quickly set up and starts the database schema migrations. The reason we require database migrations can be explained as follows. Suppose we build a database, and then require it to be modified by adding an extra column. Post addition we feel that the schema now present doesn’t fit well into the full application architecture and would like to return back to the original one. In a normal case it is difficult to do so, but with flask migrate the tasks are much smoother! In this article, we will look at ways on how we perform migration in Flask.

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

Syntax

Installing flask migrate and configuring it:

pip install flask-migrate

Installing SQLAlchemy in Flask:

pip install flask-sqlalchemy

Configuring the Database URI in configuration parameter:

Initializing Migrate command:

Creation of migration repository:

flask db init

Creation of initial migration:

flask db migrate How to perform migrate in Flask?

Before we think about performing migrate in Flask, we need to make sure that the module is installed in the python environment. For that, we would need to run the command pip install flask-migrate which will ensure that the module is installed in the environment where the python code will be running from. One needs to also make sure that the SQL alchemy module is also installed for smooth running of the migrate in Flask. Once everything is set, we are ready for the next steps in performing migrate in Flask.

Flask applications sometimes feel the necessity to dynamically using or insert their own settings in the Alembic configuration. For taking care of the utility, we use the decorator utility which uses the configure callback. Post the decorator utility and callback a function is defined. This function can modify the configuration object or completely replace it with the new variable within the function. Using decorator allows usage of multiple configuration callbacks and the order in which the callbacks are invoked are undetermined. Once the configuration piece is sorted, it would be time for binding the databases. The installation of the SQL alchemy module provides features to allow Flask-migrate to track migrations to multiple databases. This is possible with the binds feature of SQL alchemy. Including –multidb argument into the command enables the creation of multiple database migration repositories.

The flow follows as:

from flask_script import Manager from flask_migrate import MigrateCommand appFlask = Flask(__name__) manager = Manager(appFlask) Examples

Let us discuss examples of Flask Migrate.

Example #1

Installation of the Flask migrate module in Python environment:

Syntax:

pip install flask-migrate

Output:

Example #2

Initializing the migrate instance:

from flask_migrate import Migrate from flask import Flask appFlask = Flask(__name__) from flask_sqlalchemy import SQLAlchemy db = SQLAlchemy(appFlask) migrate = Migrate(appFlask, db) migrate

Output:

Example #3

Adding the MigrateCommand into the list of commands in the manager:

Syntax:

from flask_script import Manager from flask_migrate import MigrateCommand from flask import Flask appFlask = Flask(__name__) from flask_sqlalchemy import SQLAlchemy db = SQLAlchemy(appFlask) manager = Manager(appFlask) manager.add_command('db', MigrateCommand) manager

Output:

Conclusion

In conclusion, in this article, we have learned about the migrate configuration and application in Flask. We can also use this methodology to include migration of an existing project as well so that developer doesn’t need to delete everything and then start from scratch. The only thing which needs to be kept in mind during the task of including migration in an existing project is some changes need to be made in the models of the source code. Like anytime else, rest is to you for experimentation.

Recommended Articles

This is a guide to Flask Migrate. Here we discuss the definition, syntax, How to perform migrate in Flask? and examples respectively. You may also have a look at the following articles to learn more –

You're reading How To Perform Migrate In Flask With Examples?

How To Use Object In Excel Vba With Examples?

VBA Object

In Microsoft Excel, a VBA Object can contain one or more than one object. Such as a single workbook can have one or more than one worksheet. Workbook, Worksheet, Range, and cells are the objects in Excel. Each object has its own properties. And they all have a different method of application. Let say the workbook is the main object which has 2 worksheets in it. Those 2 worksheets will be its child object. One of the sheets has one range, so that sheet will become the main object, and the range will be its child object.

Watch our Demo Courses and Videos

Valuation, Hadoop, Excel, Mobile Apps, Web Development & many more.

How to Use Object in VBA

Let’s see the examples of object in Excel VBA.

You can download this VBA Object Excel Template here – VBA Object Excel Template

Example #1 – VBA Object

It can be written in many ways. Suppose we need to print any text in a cell, so this can be done in various ways. We can directly use the range value to that cell. This direct method is not a part of VBA Object but a process of performing a task that could be done by VBA Object as well. For this:

Step 1: Go to VBA and insert a Module from the Insert menu option as shown below.

Step 2: Now write the Sub Category of performed function, like VBA Object, in any other name as per your choice, as shown below.

Sub

VBAObject2()

End Sub

Step 3: Now select the range function considering any cell, let’s say cell B3 with Value as shown below.

Code:

Sub

VBAObject2() Range("B3").Value =

End Sub

Step 4: Now add text or word in that range cell as shown below.

Code:

Sub

VBAObject2() Range("B3").Value = "VBA Object"

End Sub

Step 5: Now, compile the code and run it by pressing the play button located below the menu bar.

Example #2 – VBA Object

This is the normal way of printing text to any cell. How we will see how the same process can be done when we use VBA Object. For this, we will need another fresh module. And in that,

Step 1: Go to VBA and insert a Module from the Insert menu option as shown below.

Step 2: Write the Sub Category of VBA Object as shown below.

Code:

Sub

VBAObject1()

End Sub

Step 3: Here, we will see the complete VBA Object from the main to a child category. First, select the currently opened workbook with the help of the command ThisWorkBook with Application, as shown below. This will select the workbook which is actually opened and last selected.

Code:

Sub

VBAObject1() Application.ThisWorkbook

End Sub

Step 4: Now select the sheet which is currently opened in the workbook, or we can write the name of the worksheet as well. Here, we have written the name of sheet Sheet1 in inverted commas, as shown below.

Code:

Sub

VBAObject1() Application.ThisWorkbook.Sheets ("Sheet1")

End Sub

Step 5: Now comes the range. Select the range of the cell where we need to print or insert the text. Here we are selecting cell B4, as shown below.

Code:

Sub

VBAObject1() Application.ThisWorkbook.Sheets("Sheet1").Range("B4").Value

End Sub

Code:

Sub

VBAObject1() Application.ThisWorkbook.Sheets("Sheet1").Range("B4").Value = "VBA Object"

End Sub

Step 7: Now, compile and run the code. We will see cell B4 has the text “VBA Object”.

This complete process is called VBA Object. In which we have first selected the main object, i.e. Workbook, which has its child object, i.e. Sheet1, and that has another child object range, i.e. cell B4.

Example #3 – VBA Object

There are many different ways to add text to any cell. One can be with Excel VBA Object, and others can be without it. In these categories, again, there are many ways to do it. Let’s see another way to add VBA Object. For this, we will need a module.

Step 1: In that module, add a subcategory; better make it with sequence number as shown below.

Code:

Sub

VBAObject3()

End Sub

Step 2: Select the worksheet by adding the name of the current worksheet, which is Sheet1, as shown below.

Code:

Sub

VBAObject3() Worksheets("Sheet1").

End Sub

Step 3: Now, add the range of the cell where we want to see the output, as shown below.

Code:

Sub

VBAObject3() Worksheets("Sheet1").Range("A3").Value

End Sub

Step 4: At last, give it a value that we can see once we run the code. We are considering the same text as seen in example 1.

Sub

VBAObject3() Worksheets("Sheet1").Range("A3").Value = "VBA Object"

End Sub

Step 5: Now run the code. We will see; cell A3 got the text which we wanted to add there, as shown below.

In this process, we have directly added the worksheet. So Worksheet will become our Object, and Range will become its child object.

Step 6: There is another way to perform the same task. In the bracket of the worksheet, instead of writing the sheet name, we can write the sequence of the sheet, which is 1, as shown below.

Code:

Sub

VBAObject3()

'

Worksheets("Sheet1").Range("A3").Value = "VBA Object"

Worksheets(1).Range("B3").Value = "VBA Object"

End Sub

Step 7: Now run the modified code. We will see cell B3 got the same text VBA Object as cell A3, as shown below.

By keeping both the code in line, we can see and compare the changes we made. In another way,

Step 8: Instead of the Worksheet function, if we use the Sheet with sequence and selecting cell C3 as range as shown below.

Code:

Sub

VBAObject3()

'Worksheets("Sheet1").Range("A3").Value = "VBA Object"

'Worksheets(1).Range("B3").Value = "VBA Object"

Sheet1.Range("C3").Value = "VBA Object"

End Sub

Step 9: Now run this code. We will see, again the same text will get added in range cell C3.

In all the methods which we have seen in this example, Worksheet is our object, and the range of the cell is child object or Sub-object.

Pros and Cons of  Excel VBA Object

We can make as many objects and link them together to sink them.

It makes use of Workbook, Sheet, and Range easy.

This allows a user to make changes in a specific Workbook, Worksheet or Range.

The same process can be performed by a much shorter code with having the same result.

Things to Remember

Worksheet and Sheet both have the same use.

We can select any worksheet of the same workbook of any number sequence.

While writing big lines of code, it is important to form an Object in which the Workbook, Worksheets, and Ranges of different cells are linked.

Must save the file in Macro-Enabled Excel format. It allows us to use the code and change the code whenever required.

Recommended Articles

This is a guide to VBA Object. Here we discuss how to use Object in Excel VBA along with practical examples and downloadable excel template. You can also go through our other suggested articles –

How To Use Isblank With Examples

ISBLANK Function

Checks if a specified cell is blank or not

Written by

CFI Team

Published July 2, 2023

Updated July 7, 2023

What is Excel ISBLANK Function?

The ISBLANK Function[1] is an Excel Information function that returns true if the argument cell has no information in it. ISBLANK checks a specified cell and tells us if it is blank or not. If it is blank, it will return TRUE; else, it will return FALSE. The function was introduced in MS Excel 2007.

In financial analysis, we deal with data all the time. The ISBLANK function is useful in checking if a cell is blank or not. For example, if A5 contains a formula that returns an empty string “” as a result, the function will return FALSE. Thus, it helps in removing both regular and non-breaking space characters.

However, if a cell contains good data, as well as non-breaking spaces, it is possible to remove the non-breaking spaces from the data.

Formula

=ISBLANK(value)

Where:

Value (required argument) is the value that we wish to test. (This function takes in a cell)

How to use the Excel ISBLANK Function

As a worksheet function, ISBLANK can be entered as part of a formula in a cell of a worksheet. To understand the uses of this function, let us consider a few examples:

Highlight Missing Values – Example

Suppose we are given the following data:

Suppose we wish to highlight cells that are empty. We can use the ISBLANK coupled with conditional formatting. For example, suppose we want to highlight the blank cells in the range A2:F9, we select the range and use a conditional formatting rule with the following formula: =ISBLANK(A2:F9).

How to do conditional formatting?

The input formula is shown below:

We will get the results below.

Conditional formatting didn’t highlight cell E5. After checking, there is a formula inserted into the cell.

The Excel ISBLANK function will return TRUE when a cell is actually empty. If a cell is an empty string (“”), ISBLANK will return FALSE, as it is not technically blank,  and it won’t be highlighted as shown above.

Extracting the first NON-Blank value in an array

Suppose we wish to get the first non-blank value (text or number) in a one-row range. We can use an array formula based on the INDEX, MATCH, and ISBLANK functions.

We are given the data below:

Here, we want to get the first non-blank cell, but we don’t have a direct way to do that in Excel. We could use VLOOKUP with a wildcard *, but that will only work for text, not numbers.

Hence, we need to build the functionality by nesting formulas. One way to do it is to use an array function that “tests” cells and returns an array of TRUE/FALSE values that we can then match with MATCH. Now MATCH looks for FALSE inside the array and returns the position of the first match found, which, in this case, is 2. Now the INDEX function takes over and gets the value at position 2 in the array, which, in this case, is the value PEACHES.

As this is an array formula, we need to enter it with CTRL+SHIFT+ENTER.

We get the results below:

Additional Resources

Thanks for reading CFI’s guide to important Excel functions! By taking the time to learn and master these functions, you’ll significantly speed up your financial modeling and valuation analysis. To learn more, check out these additional CFI resources:

Learn How Untar Works In Ansible With Examples

Introduction to Ansible untar

Ansible untar module also known as unarchive for Unix OS or win_zip for the windows operating system is used to extract or unpack the archive files or folders on the remote destination by copying the archive file first by default on the remote server and also supports many parameters to deal with the unarchive or win_zip functionality like the owner, dest, exclude and more.

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

Syntax

For the non-windows target unarchive or ansible.builtin.unarchive command uses the below syntax. We have shown the basics parameters

Parameters:

copy: No / Yes (Default)

The default Option is Yes and it copies the files from the local controller to the remote host if specified yes or nothing specified. For working with the remote source need to specify the remote_src and this copy parameter is not helpful there.

src (required):

Source path of the file. If remote_src is ‘yes’ then the source path would be on the remote location otherwise the source path is on the local controller node.

dest (required):

Remote server absolute path where the archive is going to extract.

remote_src: no (Default) / yes

If set ‘yes’ specifies that the source archive path is on the remote computer. For the ‘no’ value the path is on the local controller node.

owner:

name of the user that should own the files/directory after unpack and same replicates (added) to the chown.

group:

Name of the group that should own the files/directory and same replicates (added) to the chown.

exclude:

List of files or directories that need to be excluded from the unpacking.

include:

List of files or directories that need to be included in the unpacking process.

keep_newer: no (default) / yes

If set yes, it replaces the existing files that are newer than the files inside the archive.

mode:

Permissions that the files or directories should have after unpacked (like 644, 777, etc).

For the windows target src, dest, creates parameters remain the same as unarchive module and the additional parameters are as below.

Parameters:

delete_archive: no (default) / yes

Removes the zip file after unzipping.

password

If the Zip file is password encrypted then this parameter is useful and it requires the PSCX module to be installed.

recurse: no (default) / yes

recursively expands the zip file within the src directory.

Setting value ‘yes’ requires the PSCX module to be installed.

Please note: the win_unzip module doesn’t use the remote_src parameter.

How untar works in Ansible?

Ansible unarchive module for non-windows target comes with the ansible-base and so it is included in the Ansible installations. For the windows target, we need to use the win_unzip module. If the module is not available then you can download it from the galaxy.

ansible-galaxy collection install community.windows

Below is the simple playbook for windows untar (unzip).

dest: c:temp7zipInstaller

Output:

The above playbook will unzip the file from the source location to the destination remote servers. If the destination path doesn’t exist it creates the destination path.

You must have noticed here, although the source path is remote, we don’t need to use remote_src because the win_unzip module doesn’t support it and when we use the remote source path for the non-target windows server we must specify the remote_src. For example,

remote_src: yes

Examples

Here are the following examples mention below

Example #1 – unarchive module task for UNIX os.

dest: /tmp/phyes

In this playbook, it will retrieve the chúng tôi file to the destination remote host /tmp/phyes. Before running this task unlike the win_unzip module, you need to make sure that the destination path exists otherwise it will throw an error.

Output:

Example #2 – Exclude files from the archive.

To exclude the certain file from the unpacking we can use h

Code:

exclude: ’Test1 document.txt’

If there are multiple files to exclude then you can provide the list as below.

- ’sourcefile.py’

Example #3 – Include files from the archive

Code:

include: ’Test1 document.txt’

The above playbook will include only the test1 text file and to include the multiple documents, use the below command.

- ’sourcefile.py’

Example #4 – using multiple parameters together.

Code:

owner: ansibleadmin

In the above playbook, it will include only 2 files, keep_newer parameter will not replace the existing files that are newer than files from the archive, remote_src indicates the remote source and it will set the owner permission on the files and it will keep the file permission 0644.

Conclusion

Untar or unarchive or unzip modules are very useful when we write the playbook. It makes it easier to extract the files or folders on the destination server without using any third-party software and in addition, it uses various parameters like we don’t need to copy the file before extracting, adding permissions after extract, etc.

Recommended Articles

This is a guide to  Ansible untar. Here we discuss How untar works in Ansible and Examples along with the codes and outputs. You may also look at the following articles to learn more –

How Do Variables Work In Xslt With Examples?

Definition of XSLT Variable

XSLT variable is defined as special tags used to declare a local or global variable that we make use of to store any values. The declared variables are referenced within an Xpath expression. Once it is set we cannot overwrite or update the variables. The scope of the element is done by the element that contains it. While we are setting a variable it could be done as global and local variables. The top-level element declared in the file is named as a global variable and the local variables are assigned within the template section.

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

Syntax:

Following is the syntax declaration of the variable element.

How do variables work in XSLT?

The following templates assign the variable which includes both text and parameter values. The output is generated by referring to the value of the variable.

A variable takes a lot of types of values like integer or declared within XPath type expressions.

With Integer

With String

With Input document

For instance, let’s take a scenario like we need to output the book title of a respective Document in various places. We can do with the XSL variable and the book title is changed provided the changes done in one preferred location in the XSLT file. For example, we may need to use the value of current-time twenty times. Instead of passing a call to current-time(), we can call once and the corresponding value is stored in a variable.

Examples

Below example create a demo on two files  XSL and XML  with their elements and their child elements in the XML file and matches them with the XSL variable name. Let’s get started with the first demo.

Example #1 – Here is our XML file taking Book details

Following this file we have XSL style sheets with a template match by assigning a variable name to the values.

Explanation

Here an XML file is converted into a new XML file by assigning a variable name simultaneously  I have incremented the variable many times when need with the new variable.  The above code uses a global variable and could be accessed throughout the chúng tôi supports three modes of Output methods XML, HTML and Text. Here I have used HTML to show. Well, we can see the output like this:

Output:

Example #2

XML file

Xslt file

Explanation

As a result of adding this stylesheet and applying this rule of variable assignments, the output is generated. The resulting Output is given as follows:

Output:

Example #3

Xml file

XSL file

Here is a page

Explanation:

The above code uses three variable names and all the values are been assigned.

Output:

Advantages

XSL variables are very useful in many circumstances.

Variables help in avoiding long typing of XPath expression in case of complicated instructions.

XSL Being a Formatting language used for many XML Applications by providing elements and variable names with local or global declarations where they exclusively focus on formatting Objects.

Conclusion

XSLT is gaining much importance in business logic and produces a few best practices in achieving a good result in XSL. Therefore in this article, we have seen how variables are declared with the example.

Recommended Articles

This is a guide to XSLT Variable. Here we discuss the definition, syntax, How do variables work in XSLT? examples with code implementation. You may also have a look at the following articles to learn more –

How To Install Flask On Ubuntu 18.04 With Uwsgi, Nginx On Google Cloud

Introduction

How To Install Flask on Ubuntu 18.04 with uWSGI, Nginx on Google Cloud

Flask is an open source micro framework for Python.

In this guide, I will demonstrate how to install and configure Flask, uWSGI and Nginx on Ubuntu 18.04 LTS.

Prerequisites

Your Compute Engine Instance running, see the Setting up Compute Engine Instance.

Domain name is pointed to your virtual machine.

For setting up Cloud DNS, see the Setting up Google Cloud DNS for your domain.

Install required packages

SSH to your Compute Engine instance and begin typing the following commands to start installing Flask

sudo apt update sudo apt install python3-pip python3-dev build-essential libssl-dev libffi-dev python3-setuptools nginx curl Creating a Python Virtual Environment for Flask sudo -H pip3 install --upgrade pip sudo -H pip3 install virtualenv mkdir ~/

myprojectdir

cd ~/

myprojectdir

virtualenv

myprojectenv

Activate the virtual environment by typing

source

myprojectenv

/bin/activate

Your prompt should change to indicate that you are now operating within a Python virtual environment. It will look something like this: (myprojectenv)[email protected]:~/myprojectdir$

Setup Flask pip install wheel pip install uwsgi flask

Create a Sample Flask App

sudo nano ~/

myprojectdir

/myproject.py

Paste the following

from flask import Flask app = Flask(__name__) @app.route("/") def hello(): if __name__ == "__main__": app.run(host='0.0.0.0')

Hit Ctrl+X followed by Y to save the file

deactivate Create Socket and Service files for uWSGI sudo nano ~/

myprojectdir

/wsgi.py

Now import the Flask instance from your application

from myproject import app if __name__ == "__main__": app.run()

Create a uWSGI configuration file for long-term usage

sudo nano ~/

myprojectdir

/myproject.ini [uwsgi] module = wsgi:app master = true processes = 5 socket = myproject.sock chmod-socket = 660 vacuum = true die-on-term = true

Now create the systemd service unit file to automatically start uWSGI and serve the Flask application whenever the instance boots.

sudo nano /etc/systemd/system/myproject.service [Unit] Description=uWSGI instance to serve myproject After=network.target [Service] User=

username

Group=

username

WorkingDirectory=/home/

username

/

myprojectdir

Environment="PATH=/home/

username

/

myprojectdir

/

myprojectenv

/bin" ExecStart=/home/

username

/

myprojectdir

/

myprojectenv

/bin/uwsgi --ini myproject.ini [Install] WantedBy=multi-user.target

Start and enable your configuration

sudo systemctl start myproject sudo systemctl enable myproject sudo systemctl status myproject NGINX Proxy Pass to uWSGI and setup HTTPS

Edit your nginx.conf and replace user www-data with user username

Create a new Nginx configuration for your website in the sites-available directory

Copy and paste the following configuration, ensure that you change the server_name, error_log to match your domain name. Hit CTRL+X followed by Y to save the changes.

server { listen 80; listen [::]:80; location = chúng tôi { access_log off; log_not_found off; } location / { include uwsgi_params; uwsgi_pass unix:/home/

username

/

myprojectdir

/myproject.sock; } }

To enable this newly created website configuration, symlink the file that you just created into the sites-enabled directory.

Check your configuration and restart Nginx for the changes to take effect

sudo nginx -t sudo service nginx restart

Now visit your domain name in your web browser, you can view the Flask Sample page you have created.

Create SSL certificate and enable HTTP/2 sudo add-apt-repository ppa:certbot/certbot sudo apt-get update sudo apt-get install python-certbot-nginx

Now we have installed Certbot by Let’s Encrypt for Ubuntu 18.04, run this command to receive your certificates.

sudo certbot --nginx certonly

Enter your email and agree to the terms and conditions, then you will receive the list of domains you need to generate SSL certificate.

To select all domains simply hit Enter

The Certbot client will automatically generate the new certificate for your domain. Now we need to update the Nginx config.

Redirect HTTP Traffic to HTTPS with www in Nginx

Open your site’s Nginx configuration file add replace everything with the following. Replacing the file path with the one you received when obtaining the SSL certificate. The ssl_certificate directive should point to your chúng tôi file, and the ssl_certificate_key directive should point to your chúng tôi file.

server { listen [::]:80; listen 80; } server { root /home/

username

/

myprojectdir

; } server { location = chúng tôi { access_log off; log_not_found off; } location / { proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto $scheme; proxy_redirect off; include uwsgi_params; uwsgi_pass unix:/home/

username

/

myprojectdir

/myproject.sock; add_header X-Frame-Options "SAMEORIGIN" always; add_header X-Content-Type-Options "nosniff" always; add_header Referrer-Policy "origin-when-cross-origin" always; add_header Strict-Transport-Security "max-age=31536000; includeSubdomains; preload"; } }

Hit CTRL+X followed by Y to save the changes.

Check your configuration and restart Nginx for the changes to take effect.

sudo nginx -t sudo service nginx restart Renewing SSL Certificate

Certificates provided by Let’s Encrypt are valid for 90 days only, so you need to renew them often. Now you set up a cronjob to check for the certificate which is due to expire in next 30 days and renew it automatically.

sudo crontab -e

Add this line at the end of the file

Hit CTRL+X followed by Y to save the changes.

This cronjob will attempt to check for renewing the certificate twice daily.

Enjoy your Flask installation on Ubuntu 18.04 LTS with uWSGI, Nginx and HTTPS on Google Cloud

Update the detailed information about How To Perform Migrate In Flask With Examples? on the Kientrucdochoi.com website. We hope the article's content will meet your needs, and we will regularly update the information to provide you with the fastest and most accurate information. Have a great day!