Skip to content

Instantly share code, notes, and snippets.

View ttamg's full-sized avatar

Matt G ttamg

  • London
View GitHub Profile
@ttamg
ttamg / ajax_get.js
Created May 26, 2019 17:58
Opens a modal window and fetches the Django form (in HTML) by AJAX
// Parameters ...
// Trigger: A click on a button with 'openModal' class
// Requires: modal-id as attribute on the button
// URL: fetched from the ajax-url attribute on the modal Content div
$(document).ready(function () {
// A function to run on the click event to open modal, and fetch form via GET AJAX request
$('.openModal').on('click', function () {
var modalID = $(this).attr("modal-id");
@ttamg
ttamg / ajax_post.js
Created May 26, 2019 18:00
Post the Django form by AJAX and post messages as required
// Parameters ...
// Note this should be run AFTER the form has been loaded in order for the JS to attach to the form
// Trigger: Submission of the form
// Form: must have the class 'submitByAJAX' for this to work
// URL: fetched from the ajax-url attribute on the form object
$(".submitByAJAX").each(function () {
var form = $(this).find('form');
form.submit(function (e) {
e.preventDefault();
@ttamg
ttamg / post_messages.js
Created May 26, 2019 18:01
Posts Bootstrap 4 style message to the #messages area of the DOM
// Post messages
function postSuccessMessage(message) {
html = `
<div class="alert alert-success fade show">
<button type="button" class="close" data-dismiss="alert">&times;</button>`+ message + `</div>`
$('#messages').append(html);
setTimeout(function () {
$(".alert").alert('close');
}, 3000);
}
@ttamg
ttamg / refresh.js
Created May 26, 2019 18:03
A simple refresh icon that can be added to the navbar at the #refresh id in the DOM. Call needsRefresh() to add the icon.
// Add refresh app icon
function needsRefresh() {
$('#refresh').empty();
html = `
<button class="btn" data-toggle="tooltip" title="Click to refresh app"><i data-feather="refresh-cw" onclick="refresh();"></i></button>
`
$('#refresh').append(html);
$('[data-toggle="tooltip"]').tooltip()
feather.replace()
}
@ttamg
ttamg / ajax_modal.html
Last active May 26, 2019 18:09
A modal that can fetch the Django HTML form by AJAX using the ajax_get.js script
<div class="modal fade" id="messageModal" tabindex="-1" role="dialog" aria-labelledby="messageModalLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="messageModalLabel">Post a message</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">&times;</span>
</button>
</div>
<div class="modal-body">
@ttamg
ttamg / ajax_form.html
Created May 26, 2019 18:09
Django form template to fill out the modal window and load the ajax_post.js script to manage the submission of the form
<div class="submitByAJAX">
<form method="POST" action="{% url 'message_new' %}">
{% csrf_token %}
{{ form }}
<button type="submit" value="submit" class="btn">Submit</button>
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
</form>
</div>
@ttamg
ttamg / bot_sync.py
Created June 7, 2020 15:43
Trading Bot example (synchronous)
# ORIGINAL VERSION - fully synchronous
import time
import random
import logging
logging.basicConfig(format="%(asctime)s - %(message)s", level=logging.INFO)
def get_market_data(market):
@ttamg
ttamg / unsync_usage.py
Last active June 7, 2020 16:46
Simple usage example for unsync
import time
from unsync import unsync
@unsync
def my_function():
""" A function which we want to use asynchronously """
time.sleep(1)
return "something fascinating"
@ttamg
ttamg / bot_unsync.py
Created June 7, 2020 17:06
Bot example (unsync to parallelise)
# UNSYNC VERSION - this parallelises both the run methods and
# then the API and database call methods within each run
import time
import random
import logging
from unsync import unsync
logging.basicConfig(format="%(asctime)s - %(message)s", level=logging.INFO)
@ttamg
ttamg / build-test-lambda-deploy
Created January 31, 2021 09:50
End to end job to build python app, test, upload to S3 and deploy to existing AWS Lambda
# End to end job to build, test, upload to S3 and deploy to AWS Lambda
# 1. Builds python package installing dependencies in library we can access for ZIP file
# 2. Runs tests with Pytest
# 3. Creates ZIP package for Lambda (date-stamped filename)
# 4. Uploads to your S3 bucket
# 5. Deploys to AWS Lambda function
# Runs on update to main branch
# Because tests are run, the .pyc files are included in the package (faster Lambda run times?)
name: Build, test and deploy to Lambda