Posted by: xSicKxBot - 04-16-2023, 10:54 AM - Forum: Python
- No Replies
Pandas Boolean Indexing
5/5 – (1 vote)
Boolean indexing in Pandas filters DataFrame rows using conditions. Example: df[df['column'] > 5] returns rows where 'column' values exceed 5. Efficiently manage and manipulate data with this method.
Here’s an easy example:
import pandas as pd # Create a sample DataFrame
data = {'Name': ['Alice', 'Bob', 'Charlie', 'David'], 'Age': [25, 30, 35, 40], 'City': ['New York', 'San Francisco', 'Los Angeles', 'Seattle']}
df = pd.DataFrame(data) # Perform boolean indexing to filter rows with age greater than 30
age_filter = df['Age'] > 30
filtered_df = df[age_filter] # Display the filtered DataFrame
print(filtered_df)
This code creates a DataFrame with data for four people, then uses boolean indexing to filter out the rows with an age greater than 30. The filtered DataFrame is then printed.
Let’s dive slowly into Boolean Indexing in Pandas:
Understanding Boolean Indexing
Boolean indexing is a powerful feature in pandas that allows filtering and selecting data from DataFrames using a boolean vector. It’s particularly effective when applying complex filtering rules to large datasets .
To use boolean indexing, a DataFrame, along with a boolean index that matches the DataFrame’s index or columns, must be present.
To start, there are different ways to apply boolean indexing in pandas. One can access a DataFrame with a boolean index, apply a boolean mask, or filter data based on column or index values .
For instance, boolean indexing can filter entries in a dataset with specific criteria, such as data points above or below a certain threshold or specific ranges .
Working with boolean indexes is pretty straightforward. First, create a condition based on which data will be selected. This condition will generate a boolean array, which will then be used in conjunction with the pandas DataFrame to select only the desired data .
Here’s a table with examples of boolean indexing in pandas:
Example
Description
df[df['column'] > 10]
Select only rows where 'column' has a value greater than 10.
df[(df['column1'] == 'A') & (df['column2'] > 5)]
Select rows where 'column1' is equal to 'A' and 'column2' has a value greater than 5.
df[~(df['column'] == 'B')]
Select rows where 'column' is not equal to 'B'.
How Boolean Indexing Works in Pandas
Boolean indexing in Pandas is a technique used to filter data based on actual values in the DataFrame, rather than row/column labels or integer locations. This allows for a more intuitive and efficient way to select subsets of data based on specific conditions. Let’s dive into the steps on how boolean indexing works in Pandas:
Creating Boolean Arrays
Before applying boolean indexing, you first need to create a boolean array. This array contains True and False values corresponding to whether a specific condition is met in the DataFrame.
In this example, we create a boolean array by checking which elements in column 'A' are greater than 2. The resulting boolean array would be:
[False, False, True, True]
Applying Boolean Arrays to DataFrames
Once you have a boolean array, you can use it to filter the DataFrame based on the conditions you set. To do so, simply pass the boolean array as an index to the DataFrame.
Let’s apply the boolean array we created in the previous step:
filtered_df = df[bool_array]
This will produce a new DataFrame containing only the rows where the condition was met, in this case, the row that had values greater than 2:
A B
2 3 7
3 4 8
To provide more examples, let’s consider the following table:
Boolean Condition
DataFrame[boolean_array]
df['A'] >= 3
A B 2 3 7 3 4 8
df['B'] < 8
A B 0 1 5 1 2 6 2 3 7
(df['A'] == 1) | (df['B'] == 8)
A B 0 1 5 3 4 8
(df['A'] != 1) & (df['B'] != 7)
A B 1 2 6 3 4 8
Filtering Data with Boolean Indexing
Boolean indexing is also a powerful technique to filter data in Pandas DataFrames based on the actual values of the data, rather than row or column labels . In this section, you’ll learn how to harness the power of boolean indexing to filter your data efficiently and effectively.
Selecting Rows Based on Condition
To select rows based on a condition, you can create a boolean mask by applying a logical condition to a column or dataframe. Then, use this mask to index your DataFrame and extract the rows that meet your condition . For example:
In this example, the mask is a boolean Series with True values for rows with A > 2, and filtered_data is the filtered DataFrame containing only the rows that meet the condition.
Combining Conditions with Logical Operators
For more complex filtering, you can combine multiple conditions using logical operators like & (AND), | (OR), and ~ (NOT). Just remember to use parentheses to separate your conditions:
This filters the data for rows where both A > 2 and B < 8.
Using Query Method for Complex Filtering
For even more complex filtering conditions, you can use the query method. This method allows you to write your conditions using column names, making it more readable and intuitive:
Example:
filtered_data3 = df.query('A > 2 and B < 8')
This achieves the same result as the masked2 example, but with a more readable syntax.
Pandas Boolean Indexing Multiple Conditions
Here is a table summarizing the examples of boolean indexing with multiple conditions in Pandas:
Example
Description
df[(df['A'] > 2) & (df['B'] < 8)]
Rows where A > 2 and B < 8
df[(df['A'] > 2) | (df['B'] < 8)]
Rows where A > 2 or B < 8
df[~(df['A'] > 2)]
Rows where A is not > 2
df.query('A > 2 and B < 8')
Rows where A > 2 and B < 8, using query method
With these techniques at your disposal, you’ll be able to use boolean indexing effectively to filter your Pandas DataFrames, whether you’re working with simple or complex conditions .
Modifying Data Using Boolean Indexing
Boolean indexing is also great to modify data within a DataFrame or Series by specifying conditions that return a boolean array. These boolean arrays are then used to index the original DataFrame or Series, making it easy to modify selected rows or columns based on specific criteria.
In essence, it allows you to manipulate and clean data according to various conditions. It’s perfect for tasks like replacing missing or erroneous values, transforming data, or selecting specific data based on the criteria you set. This process is efficient and versatile, allowing for greater control when working with large datasets.
Now, let’s take a look at some examples of Boolean indexing in pandas to get a better understanding of how it works. The table below demonstrates various ways of modifying data using Boolean indexing:
These examples showcase some basic boolean indexing operations in pandas, but it’s worth noting that more complex operations can be achieved using boolean indexing too. The key takeaway is that this powerful technique can quickly and efficiently modify your data, making your data processing tasks simpler and more effective.
So, next time you’re working with data in pandas, don’t forget to employ this nifty technique to make your data wrangling tasks more manageable and efficient. Happy data cleaning!
Advanced Applications
Boolean indexing in Pandas has a wide range of advanced applications, allowing users to harness its power in complex scenarios. In this section, we will dive into a few of these applications, exploring their usefulness and demonstrating practical examples.
Using Indexers with Boolean Indexing
Combining indexers like iloc and loc with boolean indexing enhances the ability to select specific data subsets. Utilizing indexers in conjunction with boolean indexing allows you to specify both rows and columns, maintaining that sweet balance of performance and flexibility.
Handling Missing Data with Boolean Indexing
Dealing with missing data can be quite challenging. However, boolean indexing in Pandas comes to the rescue. With boolean indexing, users can quickly filter out missing data by applying boolean masks. This makes data cleaning and preprocessing a breeze. No more headaches navigating through messy data!
Pandas Boolean Indexing MultiIndex
MultiIndex, also known as a hierarchical index, adds another layer of depth to boolean indexing. By incorporating boolean indexing with MultiIndex DataFrames, you can access and manipulate data across multiple levels, enhancing your data exploration capabilities.
Here’s an example demonstrating the use of a MultiIndex in combination with boolean indexing in Pandas:
import pandas as pd # Create a sample DataFrame with MultiIndex
index = pd.MultiIndex.from_tuples([('A', 1), ('A', 2), ('B', 1), ('B', 2)], names=['Category', 'Subcategory'])
data = {'Value': [10, 15, 20, 25]}
df = pd.DataFrame(data, index=index) # Perform boolean indexing to filter rows where 'Category' is 'A' and 'Value' is greater than 12
category_filter = df.index.get_level_values('Category') == 'A'
value_filter = df['Value'] > 12
filtered_df = df[category_filter & value_filter] # Display the filtered DataFrame
print(filtered_df)
This code creates a DataFrame with a MultiIndex consisting of two levels: 'Category' and 'Subcategory'. Then, it uses boolean indexing to filter the rows where the 'Category' is 'A' and the 'Value' column is greater than 12. The filtered DataFrame is then printed.
The output of the provided code is:
Value
Category Subcategory A 2 15
The filtered DataFrame contains only one row where the 'Category' is 'A', the 'Subcategory' is 2, and the 'Value' is 15, as this row meets both conditions specified in the boolean indexing.
Talk about leveling up your data analysis game!
Pandas Boolean Indexing DateTime
Time series data often requires efficient filtering and slicing. With boolean indexing applied to DateTime data, users can effortlessly filter their data based on specific date ranges, time periods, or even individual timestamps. You’ll never lose track of time with this powerful feature!
Examples of Boolean Indexing in Pandas
Below is a table showcasing a few examples of boolean indexing in action:
Now you have a better understanding of advanced applications with boolean indexing in Pandas! Happy data wrangling!
Pandas Boolean Indexing “OR”
In Pandas, Boolean indexing is a powerful way to filter and manipulate data using logical conditions . The “OR” operator, denoted by the symbol “|“, allows users to select rows that satisfy at least one of the specified conditions . In this section, let’s explore how the “OR” operator works with Boolean indexing in details, along with some examples .
With Pandas, users can combine multiple logical conditions using the “OR” operator by simply providing them with a “|“. This can be especially useful when working on complex data filtering tasks . Normally, the conditions are enclosed in parentheses to maintain order and group them correctly. Just remember to use the proper Boolean operator carefully!
For a better understanding, let’s take a look at the following example on how the “OR” operator works with Boolean indexing in Pandas:
import pandas as pd # Sample DataFrame
data = {'A': [1, 2, 3, 4, 5], 'B': [6, 7, 8, 9, 10]}
df = pd.DataFrame(data) # Boolean indexing using "OR" operator
result = df[(df['A'] > 3) | (df['B'] <= 7)]
In this example, we have a DataFrame with two columns ‘A’ and ‘B’, and the goal is to filter rows where the value of ‘A’ is greater than 3 or the value of ‘B’ is less than or equal to 7. The resulting DataFrame will include rows that meet either condition .
Column A
Column B
Condition
1
6
True
2
7
True
3
8
False
4
9
True
5
10
True
Pandas Boolean Indexing “NOT”
Pandas boolean indexing is a powerful tool used for selecting subsets of data based on the actual values of the data in a DataFrame, which can make filtering data more intuitive . In this section, we’ll focus on the “NOT” operation and its usage in pandas boolean indexing.
The “NOT” operation is primarily used to reverse the selection made by the given condition, meaning if the condition is initially true, it will turn false, and vice versa. In pandas, the “not” operation can be performed using the tilde operator (~) . It can be particularly helpful when filtering the data that does not meet specific criteria.
Let’s consider some examples to understand better how “NOT” operation works in pandas boolean indexing:
Example
Description
~df['column_name'].isnull()
Selects rows where ‘column_name‘ is NOT null
~(df['column_name'] > 100)
Selects rows where ‘column_name‘ is NOT greater than 100
~df['column_name'].str.contains('value')
Selects rows where ‘column_name‘ does NOT contain the string 'value'
In these examples, the tilde operator (~) is utilized to perform the “NOT” operation, which helps to refine the selection criteria to better suit our needs. We can also combine the “NOT” operation with other boolean indexing operations like “AND” (&) and “OR” (|) to create more complex filtering conditions .
Remember, when working with pandas boolean indexing, it’s essential to use parentheses to group conditions properly, as it ensures the correct precedence of operations and avoids ambiguity when combining them .
Boolean indexing in pandas provides an efficient and easy way to filter your data based on specific conditions, and mastering the different operations, such as “NOT”, allows you to craft precise and powerful selections in your DataFrames .
Pandas Boolean Indexing in List
Pandas Boolean indexing is a powerful technique that allows you to select subsets of data in a DataFrame based on actual values rather than row or column labels . This technique is perfect for filtering data based on specific conditions .
When using Boolean indexing, you can apply logical conditions using comparison operators or combination operators like & (and) and | (or) . Keep in mind that when applying multiple conditions, you must wrap each condition in parentheses for proper evaluation .
Let’s go through a few examples to better understand how Boolean indexing with lists works!
Example
Description
df[df['col1'].isin(['a', 'b'])]
Select rows where ‘col1’ is either ‘a’ or ‘b’
df[(df['col1'] == 'a') | (df['col1'] == 'b')]
Select rows where ‘col1’ is either ‘a’ or ‘b’, alternate method
df[(df['col1'] == 'a') & (df['col2'] > 10)]
Select rows where ‘col1’ is ‘a’ and ‘col2’ is greater than 10
df[~df['col1'].isin(['a', 'b'])]
Select rows where ‘col1’ is neither ‘a’ nor ‘b’, using the ‘not in’ condition
Remember, when working with Pandas Boolean indexing, don’t forget to import the pandas library, use proper syntax, and keep practicing ! This way, you’ll be a Boolean indexing pro in no time !
Pandas Boolean Indexing Columns
Boolean indexing in pandas refers to the process of selecting subsets of data based on their actual values rather than row or column labels or integer locations. It utilizes a boolean vector as a filter for the data in a DataFrame . This powerful technique enables users to easily access specific data pieces based on conditions while performing data analysis tasks .
In pandas, boolean indexing commonly employs logical operators such as AND (&), OR (|), and NOT (~) to create a boolean mask which can be used to filter the DataFrame. The process usually involves creating these logical expressions by applying conditions to one or more columns, and then applying the boolean mask to the DataFrame to achieve the desired subset .
Here’s a table showing some examples of boolean indexing with pandas:
Example
Description
df[df['A'] > 2]
Filter DataFrame where values in column A are greater than 2 .
df[(df['A'] > 2) & (df['B'] < 5)]
Select rows where column A values are greater than 2, and column B values are less than 5 .
df[df['C'].isin([1, 3, 5])]
Filter DataFrame where column C contains any of the values 1, 3, or 5 .
df[~df['D'].str.contains('abc')]
Select rows where column D doesn’t contain the substring ‘abc’ .
Boolean indexing is an essential tool for data manipulation in pandas, offering a versatile solution to filter and identify specific elements within the data. Harnessing the power of boolean indexing can greatly improve the efficiency of data analysis tasks, making it a valuable skill to master for users working with pandas data structures .
Pandas Boolean Indexing Set Value
In Pandas, Boolean indexing is a powerful feature that allows users to filter data based on the actual values in a DataFrame , instead of relying on their row or column labels. This technique uses a Boolean vector (True or False values) to filter out and select specific data points in a DataFrame . Let’s dive into how it works!
Using logical operators such as AND (&), OR (|), and NOT (~), Pandas makes it easy to combine multiple conditions while filtering data. Below is a table showcasing some examples of how to use Boolean indexing in Pandas to set values with different conditions:
Condition
Code Example
Setting values based on a single condition
df.loc[df['column_name'] > 10, 'new_column'] = 'Greater than 10'
df.loc[ ~(df['column_name'] < 10), 'new_column'] = 'Not less than 10'
When working with Pandas, Boolean indexing can tremendously simplify the process of filtering and modifying datasets for specific tasks . Remember that the possibilities are virtually endless, and you can always combine conditional statements to manipulate your datasets in numerous ways!
Pandas Boolean Indexing Not Working
Sometimes when working with Pandas, you may encounter issues with Boolean indexing. There are a few common scenarios that can lead to Boolean indexing not functioning as expected. Let’s go through these cases and their possible solutions.
One common issue arises when using Boolean Series as an indexer. This may lead to an IndexingError: Unalignable boolean Series provided as indexer error. This usually occurs when the Boolean mask cannot be aligned on the index, which is used by default when trying to filter a DataFrame (source).
To overcome this problem, ensure that your Boolean Series index aligns with your DataFrame index. You can use the `.loc` method with the same index as the DataFrame to make sure the Series is alignable:
df[df.notnull().any(axis=0).loc[df.columns]]
Another issue that may arise is confusion with logical operators during the Boolean indexing process. In Pandas, logical operators for Boolean indexing are different from standard Python logical operators. You should use & for logical AND, | for logical OR, and ~ for logical NOT (source).
For example, to filter rows based on two conditions:
df[(df['col1'] == x) & (df['col2'] == y)]
Here is a table with some examples of Boolean indexing in Pandas:
Condition
Code Example
Rows with values in ‘col1’ equal to x
df[df['col1'] == x]
Rows with values in ‘col1’ less than x and ‘col2’ greater than y
df[(df['col1'] < x) & (df['col2'] > y)]
Rows where ‘col1’ is not equal to x
df[~(df['col1'] == x)]
By understanding these potential pitfalls, you can ensure smoother Boolean indexing in your Pandas projects. Good luck, and happy data wrangling!
[freebies.indiegala.com] Pixel Puzzles 2: Anime is a traditional style jigsaw puzzle game, featuring 25 hand drawn images in a Kawaii style, with each puzzle piece uniquely shaped in a way no physical puzzle could be. [freebies.indiegala.com]
In a new Grid, forgotten by its creator and left alone to evolve without User intervention, an unprecedented crime has been committed. The Repository stands at the center of this society. In the aftermath of a break-in, the future of this Grid hangs in the balance.
TRON: Identity is a visual novel adventure following Query, a detective program tasked with uncovering the mystery of what was taken and by whom. Finding yourself in a world built on unstable foundations and filled with whispered knowledge, it's up to you to question suspects and investigate your surroundings to piece together the truth.
What is AutoGPT and How to Get Started: Your Quick Guide to Success
5/5 – (1 vote)
Check out the following explainer video of AutoGPT (video source):
Understanding AutoGPT
AutoGPT is an experimental open-source application that showcases the capabilities of the GPT-4 language model. It autonomously develops and manages businesses, aiming to increase their net worth . As one of the first examples of GPT-4 running fully autonomously, AutoGPT truly pushes the boundaries of what is possible with AI source.
By using AutoGPT, you’ll be able to harness the power of artificial intelligence to generate larger-scale projects in minimal time, saving you tons of time and money . For example, it can significantly improve your website’s SEO and make it appear more active and professional source.
Getting started with AutoGPT is simple! Just follow these steps:
Visit the Auto-GPT repository on GitHub, where you’ll find all the necessary files and instructions .
Join the Auto-GPT Discord community to ask questions and chat with like-minded individuals .
Follow the project creator, Torantulino, on Twitter to stay updated on the latest developments and progress .
Remember, while initial setup may require some time, it’ll be worth it when you see the fantastic benefits AutoGPT can provide to your content creation process . Happy experimenting!
Getting Started with AutoGPT
AutoGPT is an advanced language model based on the GPT-3.5 architecture, designed to generate high-quality text with minimal user input . In this section, we’ll guide you through the process of getting started with AutoGPT, covering prerequisites and preparation, along with installation and configuration. Let’s dive in!
Prerequisites and Preparation
Before you start with the installation, ensure that you have the following prerequisites in place:
A computer with internet access
Python installed (latest stable version)
Access to GitHub for downloading the AutoGPT repository
Once you have all of these in place, it’s time to prepare your computer for the installation. Begin by navigating to the directory where you want to download AutoGPT. If you prefer to use a virtual environment for Python projects, go ahead and activate it. With your environment set up, you’re ready to install AutoGPT.
Installation and Configuration
Installing AutoGPT is a breeze ! To get started, clone the AutoGPT repository from GitHub:
Next, navigate to the newly created Auto-GPT directory and install the required dependencies. This can usually be done using a single command, such as:
pip install -r requirements.txt
Once the dependencies are installed, you’re all set to start using AutoGPT ! Remember to refer to the AutoGPT GitHub page for additional information and documentation, which will help you make the most of this powerful tool .
That’s it! You’ve successfully set up AutoGPT on your computer. Now go ahead and start exploring the amazing potential of this AI-driven language model .
10 Real-World Applications and Use Cases
AutoGPT offers a variety of practical applications that can simplify your daily tasks and enhance your projects. Here are ten real-world use cases where you can take advantage of its capabilities:
Autonomous Coding and Debugging : Leverage the power of AI to write and debug code faster and more efficiently than ever before.
Social Media Management : Unleash AutoGPT as a Twitter bot that can autonomously generate and post engaging content to grow your online presence.
Content Creation : Boost your writing skills by using AutoGPT for creative tasks like storytelling, article writing, or even poetry.
Better Decision Making : Make use of informed predictions and insights offered by AutoGPT to assist you in personal and professional decision-making.
Custom Chatbots : Build intelligent chatbots that can interact with users and provide informative responses tailored to their needs.
Language Translation : Break down language barriers by utilizing AutoGPT to translate text across multiple languages with ease.
Online Tutoring : Provide support and assistance to students with their studies by employing AutoGPT as a virtual learning companion.
Email Management : Streamline your inbox by enlisting AutoGPT to sort through emails, flagging important messages, and automatically replying to routine inquiries.
Simulation and Modeling : Improve your understanding of complex systems by harnessing AutoGPT’s ability to simulate and analyze various scenarios.
By incorporating AutoGPT into your work, you can unlock new possibilities and take your projects to the next level. Get started today and see the amazing benefits that await you!
[www.indiegala.com] Add to your collection & get a selection of games made with heart and mind brought to you by Whale Rock Games for the passionate gamers! The Cyber Whale 3 Bundle is LIVE!
[www.indiegala.com] Pure of heart and purpose, the Adepta Sororitas are raised from infancy to worship only the Emperor. Known as the Sisters of Battle, they heroically stride through the thickest fighting, shielded by their fanatical devotion and tempered by discipline at the peak of human capability. https://www.youtube.com/watch?v=QpnSxSgTuTc&ab_channel=SlitherineGames
Rendezvous is a 2.5D cyber-noir pixel art action-puzzle adventure game of family, betrayal, and dangerous secrets set against the backdrop of Bay City and Neo-Surabaya.
The story follows Setyo, a former criminal agent now living a mundane life as a security technician in Bay City. When an old companion turns up one evening at the local bar and reveals Setyo's sister is working with the most dangerous Cyberrunner group in Neo-Surabaya, Setyo realizes he must face his own sins and return to save what's left of his family.
Can he save his sister from her future? And can he save himself from his past?
How I Used the Flask Framework to Create an URL Shortener Application
5/5 – (1 vote)
A URL shortener service generates a shorter, more readable version of the URL it was given. Flask, a Python web framework can be used to create a URL shortener app.
So, we will create an application allowing users to enter a URL and shorten it. We will use the SQLite database engine to store application data. If you prefer to learn how this is done using the Django framework, you are free to read this article.
Set up
Create a new folder for this project. Then, create and activate a virtual environment by running the following commands in your terminal.
The hashids library will be used to generate a unique ID. You will understand this as we proceed.
Creating a Database Engine
Since we will store application data, we must create a database file. Do it and call the file schema.sql. Then, write the following SQL commands.
DROP TABLE IF EXISTS urls;
CREATE TABLE urls( id INTEGER PRIMARY KEY AUTOINCREMENT, created TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, original_url TEXT NOT NULL, clicks INTEGER NOT NULL DEFAULT 0
);
If the above code seems strange, you may want to familiarize yourself with SQL commands.
We want to create a table named urls. As we don’t want to face issues caused by several tables with the same name, we must first delete it. That’s what is meant by ‘DROP TABLE…’
The table is then created with four columns. The id column will contain the unique integer value for each entry. Next is the date the shortened URL was generated. The third column is the original URL. Finally, the number of times the URL was clicked.
The schema.sql file can only be executed with the help of a Python script. So, we create another file called init_db.py
import Sqlite3 connection = Sqlite3.connect('database.db') with open('schema.sql') as sql: connection.executescript(sql.read()) connection.commit()
connection.close()
Once you run the script (with python3 init_db.py), a new file called database.db will be created. This is where all application data will be stored.
The connect() method creates the file. As soon as the file is created, it is then populated with the urls table. This is done by first opening and reading the content from the schema.sql.
It then calls the executescript() method to execute all the SQL commands in the SQL file. After which, we commit and close the file. By now, your folder should contain the following files:
database.db init_db.py schema.sql
Creating the Database Connection
Let us open a connection to the database file. Create a file and name it db_connection.py.
Notice that we set the row-factory attribute to sqlite3.Row. This makes it possible to access values by column name. We then return the connection object, which will be used to access the database.
The Main File
Next, create another file and name it main.py. This will be our main file. In this file, we will import the database connection file.
from db_connection import get_db_connection
from hashids import Hashids
from flask import Flask, flash, render_template, request, url_for, redirect app = Flask(__name__)
app.config['SECRET_KEY'] = 'Your secret key' hashids = Hashids(min_length=4, salt=app.config['SECRET_KEY']) @app.route('/', methods=('GET', 'POST'))
def index(): conn = get_db_connection() if request.method == 'POST': url = request.form['url'] if not url: flash('The URL is required!') return redirect(url_for('index')) url_data = conn.execute('INSERT INTO urls (original_url) VALUES (?)', (url,)) conn.commit() conn.close() url_id = url_data.lastrowid hashid = hashids.encode(url_id) short_url = request.host_url + hashid return render_template('index.html', short_run=short_url) return render_template('index.html')
We create an instance of the Flask class. The __name__ variable allows Flask to locate other resources, including templates in the current directory. We then create hashids object that will have four characters. (You can choose to have more characters). We use a secret key to specify the salt for the Hashids library.
The index() function is decorated with the @app.routedecorator that assigns the URL ('/') to the function, thus turning it into a Flask view function.
In the index() function, we open a database connection. Then, we check if the request method is POST. If so, the code block under it will be executed. If not, we only return an empty web page using the render_template() method.
If the request method is POST, we use request.form['url'] to collect input from the template file (index.html). The output is the URL to shorten. However, if the user gives no URL, we simply flash a message and redirect the user back to the same index.html web page.
If a URL is given, it will be added to the database by executing the command, INSERT INTO …
After closing the database, we select the last row id of the database, which is the current URL added. Remember the AUTOINCREMENT keyword in the id column of the database file. This ensures that the id is incremented with each new entry.
With the last row id selected, we use the hashids.encode() method to generate a unique hash and concatenate it to the URL of the application’s host (indicated with the request.host_url attribute). This becomes the shortened URL that would be displayed to the user.
Please check my GitHub page for the template files. Make sure you create a templates folder to keep the HTML files.
The local server is opened when you run python3 main.py in your terminal. This is possible because of the special name variable and the app.run() method.
Adding Extra Features
Won’t it be nice to know how many times each URL has been clicked and have them displayed on a web page? We are going to add that feature. Update your app.py by adding the following:
We again open a database connection, and fetch all the columns in the urls table (indicated by *) and a list of all the rows using the fetchall() method.
After closing the database, we loop through the result. In each iteration, we convert the sqlite3.Row object to a dictionary and repeat the same thing we did previously to encode the id number. This is then concatenated to form a new URL. Finally, we append the result to an empty list and render it to the browser.
Notice we didn’t commit the database as we did previously. This is because we didn’t make changes to the database. We close it after fetching the data we needed.
Your folder should now have the following files:
database.db,
db_connection.py,
init_db.py,
main.py,
schema.sql, templates.
Templates Files
As earlier stated, you should check my GitHub page for the templates files. We created a base.html file inside the templates folder that other files will inherit.
The other two files have certain things that make rendering dynamic content to our Flask web page possible. It is the {% ... %} and { ... } code blocks.
These are Jinja2 templating language that comes together with the Flask library.
The render_templates() method in the stats() function has another argument, urls. This is from the stats.html web page, while the other urls is the variable that will be displayed on the web page.
Conclusion
This is one of the ways to create a URL shortener app using the Flask framework. This project has expose us to how Flask works as well as how it interacts with database. If you struggle to understand some of what we did, that should be expected as a beginner. However, as you keep working on projects, it will become second nature to you.
[freebies.indiegala.com] Being able to control fate would be a blessing for many. To Miyon, it is her greatest curse. East and West Taria are in conflict, and Miyon's power is the key to their victory. Meanwhile, Gai has been sold by his family into Miyon's temple. Will the two save or destroy one another? [freebies.indiegala.com]
Redfall is an open-world, single-player, and co-op FPS from Arkane Austin, the award-winning team behind Prey and Dishonored. Continuing Arkane’s legacy of carefully crafted worlds and immersive sims, Redfall brings the studio’s signature gameplay to this story-driven action shooter. https://www.youtube.com/watch?v=whLDpLEP9To&ab_channel=BethesdaSoftworks
Compete in PGA TOUR events like all three legs of the FedExCup Playoffs and THE PLAYERS Championship. Additional experiences include the LPGA, U.S. Amatuer Championship, and the Korn Ferry Tour. EA SPORTS PGA TOUR is the only place to play the MASTERS Tournament at Augusta National Golf Club. Discover the best golfing destinations around the world and play championship courses, from Pebble Beach to St Andrews.
EA SPORTS PGA TOUR is the first-ever video game to integrate ShotLink data, resulting in unrivaled gameplay authenticity.