GithubHelp home page GithubHelp logo

nb2mail's Introduction

nb2mail - send a jupyter notebook as an email

PyPI version

This repo contains a jupyter nbconvert exporter to convert notebooks to multipart MIME, and a postprocessor to send it via smtp.

Installation

pip install nb2mail

Usage

nb2mail does not do anything by itself. It provides an export format ("mail") and postprocessor ("SendMailPostProcessor"). Please see the nbconvert documentation and example configuration for more information.

Example

To generate a mail and send it later with another process (eg sendmail):

jupyter nbconvert --execute --to mail notebook.ipynb

SMTP Example

To convert and send a mail via gmail, you can set the environment variables and declare a postprocessor with --post:

export [email protected] GMAIL_USER=user GMAIL_PASS="*****"
jupyter nbconvert --to mail --post=nb2mail.SendMailPostProcessor notebook.ipynb

Alternatively, you can configure the SMTP settings in a config file config.py:

c = get_config()
c.NbConvertApp.export_format = 'mail'
c.Exporter.preprocessors = 'nbconvert.preprocessors.ExecutePreprocessor'
c.NbConvertApp.postprocessor_class = 'nb2mail.SendMailPostProcessor'
c.SendMailPostProcessor.recipient = '[email protected]'
c.SendMailPostProcessor.smtp_user = 'user'
c.SendMailPostProcessor.smtp_pass = '*******'
c.SendMailPostProcessor.smtp_addr = 'smtp.gmail.com'
c.SendMailPostProcessor.smtp_port = 587

and then run:

jupyter nbconvert --config config.py demo.ipynb

3rd party email distributor Example

Instead of using SMTP to send emails, one can use 3rd party provider.
This is an example for using mailgun as a 3rd party provider

Configuring Mail Headers

In the notebook metadata, you can set mail headers by adding a nb2mail block:

"nb2mail": {
"attachments": [
    "business_report_attachment.xlsx"
],
"From": "[email protected]",
"To": "[email protected], [email protected]",
"Subject": "Business Report"
}

You can specify multiple recipients by seperating them with commas.

Disabling Pilcrows

Since CSS doesn't render the same in email, you may want to disable the pilcrows after each section.

c.MailExporter.anchor_link_text = '' # disable pilcrow, requires nbconvert >= 5.2

Refences

TODO

  • Prerender Math - no js in email

  • Prettier templates

  • Plotly - here is a workaround:

    # py.iplot(fig, filename=‘dcm_ctr_subplots’)
    # The above line is what you normally use to show your plots in the notebook
    # You no longer need that and just need the stuff below
    
    from IPython.display import Image
    
    py.image.save_as(fig, filename='yahoo_dcm_ctr_subplots.png')
    Image('yahoo_dcm_ctr_subplots.png')
    

nb2mail's People

Contributors

dwang0721 avatar idanre1 avatar jparmstrong avatar nfultz avatar wil avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

nb2mail's Issues

Only show stdout in email

Would it be possible to add an option so that this tool only captures stdout (or a given fd) and only puts that in the email?

This way we could have debug output going to stderr that wouldn't show up in the email.

Update to fix compatibility with nbconvert

There hasn't been a release of this library in a while, and it's not compatible with the latest version of nbconvert.

It takes a few hacks to get nb2mail to work:

     # Mime types are not compatible
     nb2mail.MailExporter.output_mimetype = ""  # Cheating
    mail_exporter = nb2mail.MailExporter(
        # For some reason nb2mail can't find its template:
        template_file=os.path.join(os.path.dirname(nb2mail.__file__), "templates", "mail.tpl"),
    )

And there are a lot of warnings:

DeprecationWarning: Keyword `traits` is deprecated in traitlets 5.0, use `per_key_traits` instead
    headers = Dict(help="Mail Headers", traits={
 DeprecationWarning: 5.x style template file passed '/Users/aandres/source/jetpump2/venv/lib/python3.9/site-packages/nb2mail/templates/mail.tpl'. Use --template-name for the template directory with a index.<ext>.j2 file and/or --template-file to denote a different template.
DeprecationWarning: Passing unrecognized arguments to super(MailExporter).__init__(output_mimetype='').
  object.__init__() takes exactly one argument (the instance to initialize)
  This is deprecated in traitlets 4.2.This error will be raised in a future release of traitlets.
    super().__init__(**kwargs)
DeprecationWarning: This template is deprecated, please use base/display_priority.j2
    warnings.warn(msg, DeprecationWarning)

change content type?

this tool is just what I need, but it doesn't quite work. the mail file that it produces has
the header:

Content-Type: multipart/alternative;

but in the result images are not displayed. just changing it to

Content-Type: multipart/mixed;

with sed fixed it for me.

This page indicates it should be mixed (at least the top level):
http://stackoverflow.com/questions/3902455/mail-multipart-alternative-vs-multipart-mixed

seems like this line is the culprit:
https://github.com/nfultz/nb2mail/blob/master/nb2mail/__init__.py#L88

care for a PR?

Configurable headers for MailExporter

Hi Neal,

It would be very useful to allow the From and Subject headers to be exposed as a configuration in MailExporter. That way, there could be a sane defaults without having to edit the notebook metadata. Configuration parameters can also be specified on the nbconvert command line making it easy to override at runtime.

I have a working patch to do that. Would you be interested in a pull request?

Thanks.
Wil

Multiline text loses line breaks

In notebooks that print multiple lines of text in the output of a cell, the conversion generates a single <p> element with all the text. However, by default newlines are not shown by <p> unless it has style white-space: pre-wrap, therefore, the resulting HTML page lacks the line breaks.

This problem does not happen when invoking nbconvert with --to html. There, the output is converted to a <pre> element.

I tried to find the cause in the code, but I am not familiar with the internals of nbconvert, so I couldn't find anything. Can you help? Thanks.

"None" in email

Hello! I've come across bug with the recent versions of nbconvert that causes None to appear on the email.

It's because of the mail.tpl extends a deprecated display_priority.tpl file, it should be base/display_priority.j2

README should provide usage information

I'm intrigued but I don't see how to use this module (I looked on pypi too).

As a minimum you should detail arguments to use and an example (from command-line I suppose).

Would be good to point to the pypi page also.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.