plagarism checker GPT
Here is an example code
Here is an explanation of each line of code
from flask import Flask, request, render_template, redirect, url_for, flash
import os
These lines import the necessary modules and libraries from Flask and Python's built-in os library.
app = Flask(__name__)
This line creates a new Flask web server from the Flask class. The __name__ argument tells Flask to use the current module (__name__ is a built-in Python variable that is set to the name of the current module) as the application.
def allowed_file(filename):
# Check if the file is in the allowed extensions
return '.' in filename and \
filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
This function checks if the file is in the allowed extensions. It returns True if the file is in the allowed extensions, and False otherwise.
@app.route('/', methods=['GET', 'POST'])
def index():
This line is a decorator that tells Flask which URL should trigger the index function. The index function will be called when the user navigates to the homepage ('/') and submits the form ('POST').
if request.method == 'POST':
This line checks if the request method is POST. If it is, the code inside the if block will be executed.
if request.form['text'] != '' and request.files['file'].filename == '':
This line checks if the text form field is filled and the file input is empty. If both conditions are True, the code inside the if block will be executed.
word = request.form['text']
masukan = "word"
with open('word.txt', 'w', encoding='utf-8') as f:
f.write(word)
These lines retrieve the value of the text form field, create a new file named word.txt, and write the value of the text field to the file.
return redirect(url_for('plagiarism', name=masukan) + "#hasil")
This line redirects the user to the plagiarism function and appends "#hasil" to the URL.
elif request.files['file'].filename != '' and request.form['text'] == '':
This line checks if the file input is filled and the text form field is empty. If both conditions are True, the code inside the elif block will be executed.
if 'file' not in request.files:
flash('No file part')
return redirect(request.url)
This line checks if the file part is present in the request. If it is not, a message is displayed using flash and the user is redirected to the current URL.
file = request.files['file']
if file.filename == '':
flash('No selected file')
return redirect(request.url)
These lines check if a file was selected. If no file was selected, a message is displayed using flash and the user is redirected to the current URL.
if file and allowed_file(file.filename):
filename = secure_filename(file.filename)
file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
return redirect(url_for('plagiarism', name=filename) + "#hasil")
These lines check if the selected file is in the allowed extensions, and if it is, the file is saved to the UPLOAD_FOLDER specified in the configuration and the user is redirected to the plagiarism function with the filename as an argument and "#hasil" appended to the URL.
else:
flash('Please fill the form')
return redirect(request.url)
This line displays a message using flash if neither the text form field nor the file input is filled, and redirects the user to the current URL.
return render_template("index.html")
This line renders the index.html template when the user navigates to the homepage.
Comments
Post a Comment