Removed_Content

Flask-Pastebin-Clone.py

Feb 20th, 2026 (edited)
110
0
Never
2
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.01 KB | Source Code | 0 0
  1. """
  2. Flask-Pastebin-Clone.py
  3.  
  4. A minimal self-hosted Pastebin clone built with Flask. Users submit a code
  5. snippet and a language through a web form; each paste is saved to a local
  6. file with a unique ID and can later be viewed with Pygments-based syntax
  7. highlighting.
  8.  
  9. Requires: flask, shortuuid, pygments
  10. Needs an index.html template (with a submission form and a paste-display
  11. section) in a templates/ folder alongside this file.
  12. """
  13.  
  14. from flask import Flask, request, render_template, abort
  15. import shortuuid
  16. import os
  17. from pygments import highlight
  18. from pygments.lexers import get_lexer_by_name, get_all_lexers
  19. from pygments.formatters import HtmlFormatter
  20.  
  21. app = Flask(__name__)
  22.  
  23. # Directory to store paste files
  24. PASTE_DIR = 'pastes'
  25. if not os.path.exists(PASTE_DIR):
  26.     os.makedirs(PASTE_DIR)
  27.  
  28. # Function to get available programming languages for syntax highlighting
  29. def get_language_options():
  30.     return sorted([(lexer[1][0], lexer[0]) for lexer in get_all_lexers() if lexer[1]])
  31.  
  32. # Route for the main page
  33. @app.route('/', methods=['GET', 'POST'])
  34. def index():
  35.     if request.method == 'POST':
  36.         # Get content and language from the form
  37.         content = request.form['content']
  38.         language = request.form['language']
  39.         # Generate a unique ID for the paste
  40.         paste_id = shortuuid.uuid()
  41.         # Create the file path for the paste
  42.         file_path = os.path.join(PASTE_DIR, paste_id)
  43.  
  44.         # Save the paste content to a file
  45.         with open(file_path, 'w') as f:
  46.             f.write(f"{language}\n{content}")
  47.  
  48.         # Generate the URL for the new paste
  49.         paste_url = request.url_root + paste_id
  50.         return render_template('index.html', paste_url=paste_url, languages=get_language_options())
  51.  
  52.     # Render the form with available languages
  53.     return render_template('index.html', languages=get_language_options())
  54.  
  55. # Route to view a specific paste by its ID
  56. @app.route('/<paste_id>')
  57. def view_paste(paste_id):
  58.     # Create the file path for the paste
  59.     file_path = os.path.join(PASTE_DIR, paste_id)
  60.     if not os.path.exists(file_path):
  61.         abort(404)  # Return a 404 error if the paste does not exist
  62.  
  63.     # Read the paste file
  64.     with open(file_path, 'r') as f:
  65.         language = f.readline().strip()  # First line is the language
  66.         content = f.read()  # Remaining content is the paste
  67.  
  68.     # Get the appropriate lexer for syntax highlighting
  69.     lexer = get_lexer_by_name(language, stripall=True)
  70.     # Create a formatter for HTML output
  71.     formatter = HtmlFormatter(linenos=True, cssclass="source")
  72.     # Highlight the content
  73.     highlighted_content = highlight(content, lexer, formatter)
  74.     # Get the CSS for the highlighted content
  75.     highlight_css = formatter.get_style_defs('.source')
  76.  
  77.     # Render the paste with syntax highlighting
  78.     return render_template('index.html', paste_content=highlighted_content, highlight_css=highlight_css, languages=get_language_options())
  79.  
  80. if __name__ == '__main__':
  81.     app.run(debug=True)
  82.  
Advertisement
Comments
Add Comment
Please, Sign In to add comment