Skip to main content

A Python package to display an HTML form in Colab, capture submissions, and send data via email.

Project description

form_submit: HTML Form Submission for Google Colab A Python package designed to simplify displaying HTML forms in Google Colab notebooks, capturing user submissions, and securely sending the collected data (including file attachments) via email.

Features Easy HTML Form Display: Render interactive forms directly within your Colab cells. Secure Email Submission: Send form data, including attached files, to a specified email address using an app password. Attachment Handling: Supports decoding and attaching base64 encoded files from the form. Installation Install the package directly from PyPI:

%pip install form_submit Usage

  1. Displaying the Form First, import the display_feedback_form function and call it in a code cell. This will render the HTML form in your Colab output.

from form_submit import display_feedback_form

display_feedback_form() 2. Handling Form Submissions To capture and process form submissions, you need to set up a JavaScript listener that sends the form data back to Python. The handle_form_submission function in this package is designed to receive this data and send an email.

Here's an example of how you might set up the JavaScript in a Colab notebook (this assumes you've displayed the form and want to handle its submission):

from google.colab import output from form_submit import handle_form_submission

Define a Python function to be called from JavaScript

def _handle_js_form_data(data): email = data.get('enter_your_email', 'N/A') full_name = data.get('enter_your_full_name', 'N/A') phone_number = data.get('enter_your', 'N/A') # Note: original form input name was 'enter_your' feedback = data.get('give_your_feedback_about_the_library', 'N/A') troubleshoots_choice = data.get('any_trobleshoots_in_the_library', 'N/A')

file_input = data.get('upload_your_poblem_in_a_file_pdf_content', None)
file_data = None
if file_input and isinstance(file_input, dict):
    file_data = {
        'filename': file_input.get('filename'),
        'content': file_input.get('content'), # Base64 encoded content
        'mimetype': file_input.get('mimetype')
    }

handle_form_submission(
    email=email,
    full_name=full_name,
    phone_number=phone_number,
    feedback=feedback,
    troubleshoots_choice=troubleshoots_choice,
    file_data=file_data
)

Expose the Python function to JavaScript

output.register_callback('handle_colab_form', _handle_js_form_data)

JavaScript to intercept form submission and send data to Python

js_code = """

<script> document.addEventListener('DOMContentLoaded', function() { const form = document.querySelector('form'); // Adjust selector if multiple forms if (form) { form.addEventListener('submit', function(event) { event.preventDefault(); // Prevent default form submission const formData = new FormData(form); const data = {}; for (let [key, value] of formData.entries()) { if (value instanceof File) { if (value.size > 0) { const reader = new FileReader(); reader.onload = function(e) { data[key] = { filename: value.name, content: btoa(e.target.result), // Base64 encode file content mimetype: value.type }; google.colab.kernel.invokeFunction('handle_colab_form', [data], {}); }; reader.readAsBinaryString(value); return; // Stop and wait for file read } } else { data[key] = value; } } google.colab.kernel.invokeFunction('handle_colab_form', [data], {});

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

form_submit-0.1.0.tar.gz (5.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

form_submit-0.1.0-py3-none-any.whl (5.7 kB view details)

Uploaded Python 3

File details

Details for the file form_submit-0.1.0.tar.gz.

File metadata

  • Download URL: form_submit-0.1.0.tar.gz
  • Upload date:
  • Size: 5.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.9.0

File hashes

Hashes for form_submit-0.1.0.tar.gz
Algorithm Hash digest
SHA256 c8ab6e1fa6d9f22b9862dce1268b224b23e28080019e6648ac3373f55b779785
MD5 9f11579fa4512be2af84a4395286cb72
BLAKE2b-256 dfbb23d9cdc4d0e318677744ac20351d2484a5da0d289579a676c88cf39efa08

See more details on using hashes here.

File details

Details for the file form_submit-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: form_submit-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 5.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.9.0

File hashes

Hashes for form_submit-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 12933512e9258d523e023d62d2d307f958877b33b9dc8233279033a201724dd2
MD5 c8901cc7872b937d3781ca66cc6a267d
BLAKE2b-256 abf051cfc2e0948832b31ef7f9169200da6da7828e5b9cb330c43a51b13323aa

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page