Thursday 23 September 2021

Mailgun: sending variables to template example - Fix ''Create and start coding'' greyed out issue.

First of all in Mailgun we'll add a template.   ( Keep reading for the ''Create and start coding'' greyed out issue fix tip ) 

So log into your account and on the dashboard you'll see all of your domains.  Click on the name of the domain that you want to add a template for.   Although there is a domain dropdown on the Template page itself, so no big deal if you want to change that later on. 

Next up, you'll need to click on 'Sending' in the left hand menu to see more options.  The 5th option down in this dropdown should be 'Templates' .



Click on this.  And then in the next page you'll get to 'Create Message template' 

You'll need to make sure the characters in the 'name' are valid and you have some sort of description.  Otherwise the 'Create and start coding' button is greyed out and you can't click on it. 


I’m sure this will trip someone else up , but the name input cannot be capitalised. If it is the Mailgun 'Create Message template' will remain greyed out.

In the HTML of template you’re setting up , you can add variable using the following.


1{{ firstname }} {{ lastname }}


How to send from Flask .

So here the function for sending a standard MIME email. With some HTML

1 def send_html_message(self): 2 return requests.post( 3 self.send_message_api, 4 auth=("api", self.mailgun_api_key), 5 data={ 6 "from": self.from_string, 7 "to": [self.to_address_2], 8 "cc": [self.to_address_1], 9 "subject": "HTML test", 10 "html": "<strong>Hello World</strong><br><p>Hi</p><p>This is an HTML test send </p>", 11 }, 12 )

 

And this is one where we use our ‘newsletter’ template and send through some variables.


1 def send_simple_message_template(self): 2 return requests.post( 3 self.send_message_api, 4 auth=("api", self.mailgun_api_key), 5 data={"from": self.from_string, 6 "to": "David Millward <dj@flowmo.co>", 7 "subject": self.subject, 8 "template": "newsletter", 9 "v:firstname":"David", 10 "v:lastname":"John", 11 })


This one will send our newsletter, with the clients name on it.






Tuesday 21 September 2021

Docker - failed to solve with frontend dockerfile.v0: failed to create LLB definition: no match for platform in manifest sha256:X: not found 7ERROR: Service 'myservice' failed to build : Build failed


docker-compose up -d
2... 3------ 4 > [internal] load metadata for docker.io/library/python:3-onbuild: 5------ 6failed to solve with frontend dockerfile.v0: failed to create LLB definition: no match for platform in manifest sha256:X: not found 7ERROR: Service 'myservice' failed to build : Build failed


I found in this post that if I ran the following lines of code in terminal before my compose command, then that would eliminate the first issue I have.


1export DOCKER_BUILDKIT=0 2export COMPOSE_DOCKER_CLI_BUILD=0 3docker-compose up -d 4 5ERROR: Service 'myservice' failed to build : no matching manifest for linux/arm64/v8 in the manifest list entries


So that second line of issue looks like it may have to do with my Mac M1 chip


1ERROR: Service 'myservice' failed to build : no matching manifest for linux/arm64/v8 in the manifest list entries



Adding the 'platform: linux/x86_64' to the following services seems to have worked.


1version: '3' 2 3services: 4 myservice: 5 platform: linux/x86_64 6 container_name: myservice_container 7 build: ./myservice 8 volumes: 9 - ./myservice:/usr/src/app 10 ports: 11 - 8080:8080


Wednesday 15 September 2021

Python Panda's - Cannot convert non-finite values (NA or inf) to integer

 In the .csv that we're processing we have a column thats 'total_in_thousands' .  

In that column the values are all in decimals and I presume the legacy code would like them to be integers. 

csv = csv.astype({'total_uv_in_thousands' : 'int64'})

However this giving me this error. 


1 astype_nansafe 2 raise ValueError("Cannot convert non-finite values (NA or inf) to integer") 3ValueError: Cannot convert non-finite values (NA or inf) to integer


To resolve this I’ve changed on NaN’s in this column to zero and it now works.
csv = csv.fillna({'total_uv_in_thousands' : 0}).astype({'total_uv_in_thousands' : 'int64'})


This works.