Giter VIP home page Giter VIP logo

Comments (7)

zedr0n avatar zedr0n commented on September 17, 2024 2

Just had a go at supporting PNG images and it seems to be working fine. See Commit

And then the code

    #r System.Drawing

    using System.Drawing;
    using System.Drawing.Imaging;

    public static partial class Test
    {
            public static string GetPng()
            {
                    using (Bitmap b = new Bitmap(50, 50)) 
                    {
                            using (Graphics g = Graphics.FromImage(b))
                            {
                                    g.Clear(Color.Green);
                            }
                            var str = new MemoryStream();
                            b.Save(str,ImageFormat.Png);
                            return System.Convert.ToBase64String(str.ToArray());
                    }        
            }
    }

can be used to output a green square in a separate cell with

 Test.GetPng()

But yes, ideally, the results of the execution should be parsed for type, mime type deduced and then encoded to base64. Doesn't sound that complicated

from icsharp.

zabirauf avatar zabirauf commented on September 17, 2024

@awb99 Thats a great suggestion. Do you have the Grid encoder in an open repo so that I can take a look at it to get a better idea of what you are proposing design wise.

from icsharp.

awb99 avatar awb99 commented on September 17, 2024

@zabirauf Not yet. I have it already, but need to factor this part out to a dedicated project. I promise to do so!

from icsharp.

awb99 avatar awb99 commented on September 17, 2024

@zabirauf https://github.com/awb99/ObjectPrinter.git
I factored out some basic functionality into this github project.
I added a feature to print a list of objects of a specific type to a html table.
For individual objects, I would rather export as a format more similar to JSON

I am not sure if we should put the dataType (jpg/html/text/...) into the ObjectPrinter; or if this should be done icsharp.

from icsharp.

zedr0n avatar zedr0n commented on September 17, 2024

Html seems to be possible as well automatically but need a proper function to check for presence of tags. See commit

var html = @"<b>Bold</b>" + "<br>" + "<i>Italic</i>";

This would output
Test
Italic

from icsharp.

zedr0n avatar zedr0n commented on September 17, 2024

Re the design concerns. While it might be nice to be able to specify explicitly in user code the return type it's a bit cumbersome dependencies wise. What would need to happen is creating a separate interfaces project which will be a common dependency of icsharp and your user code. And there's no nuget support in Ipython so instead of just distributing the kernel as an .exe file an additional dev package will need to be included plus installed to GAC as ScriptCS can only reference either those or absolute paths.

What I would rather suggest is a combination of automatic detection plus some conventions. E.g. if a function returns a Dictionary< string,object > then the first component is expected to represent the mime type. Then the conversion functions to base64 can be pulled into isharpcs directly.

Although now that I think about it one other option is maybe preinjecting the interfaces into each ScriptCS istance explicitly in ISharpCS code. Not sure if that's completely straightforward though. That could also be combined with attributes e.g.

[OutputType("text/html")]
public static string WriteHtml()
{
    return @"<b>Bold</b>" + "<br>" + "<i>Italic</i>";
}

from icsharp.

zedr0n avatar zedr0n commented on September 17, 2024

Here's also an example of directly returning HTML with embedded images

    using System.Math;

    public static partial class Test
    {
            public static string Html()
            {
                    var html = @"<img src='data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg==' alt='Red dot'></img>";
                    return html;
            }
    }

will result in a red dot displayed.

Just put all examples in a single notebook

https://github.com/zedr0n/icsharp/blob/dev/iPythonNotebook/MimeTest.ipynb

from icsharp.

Related Issues (20)

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.