We help IT Professionals succeed at work.

Browser output of HTML tables plus gnuplot image on same page without use of external files

madmax61
madmax61 asked
on
343 Views
Last Modified: 2013-12-25
In a browser running a perl CGI-BIN program(Linux) I want to call another program (FORTRAN program running on Linux) to extract data which passes back the data (after a lot of mathematical processing) to the perl program, and the result being 2 HTML tables followed by a single gnuplot plot of curves all on one page of the browser.

I can do just the HTML tables output no worries.

I think I can do the curves on a page on its own without the use of creating files on the Linux server and linking back to it from the HTML. That is generating the plot straight to the browser using:
(sample of code below)
$process=open2(GP,GP,"/usr/bin/gnuplot");
print GP "set terminal png small color");
print GP "title ...
print GP ....size, border, logscale,xrange,yrange,xlabel,ylabel,xtics,grid etc etc

print $query->header('image/png');
# initiate the plot
print GP "plot '-' using 1:2 title \"1 year\" with linespoints lt 1 pt 1 ps 2, '-' using 1:2 title ...etc


The problem with this code above is I can ONLY plot the gnuplot curve on the browser page because of the' image/png' header telling the browser the following data hitting the browser is an image. That is I cannot put the HTML tables below or above the gnuplot.

Any ideas how I can get both HTML generated tables of the data, PLUS an image generated on the fly by gnuplot?

P.S. The gnuplot module within perl is not installed on this system (I believe), and not sure if they would either. Gnuplot is installed as a standalone appilcation on the unix server though.


Note I am trying to to all this without creating files on the Linux server and then linking to it in the HTML followed by necessary file cleanup, unless you can help me with creating random filenames, and cleaning up afterwards I suppose. This program may be called by many people on a high traffic site.

Cheers,
Max



 



Comment
Watch Question

CERTIFIED EXPERT
Most Valuable Expert 2014
Top Expert 2015
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Author

Commented:
Thankyou so far, looks promising.....

Here is a sample of the type of thing I am doing:

I can't get it to work, could you please give me a little more guidance in how to get it working.


#!/usr/bin/perl
use IPC::Open2;

$process = open2(OUT, GP, "/usr/bin/gnuplot");
print GP << "end_commands";
set term png
set title "Test 2"
plot sin(x)
end_commands
close GP;
$| = 1;
print "Content_Type: image/png\n\n";
@pngdata = <OUT>;
print @pngdata;


From your link provided I need it to look similar to:

<IMG
   SRC="data:image/gif;base64,R0lGODdhMAAwAPAAAAAAAP///ywAAAAAMAAw
   AAAC8IyPqcvt3wCcDkiLc7C0qwyGHhSWpjQu5yqmCYsapyuvUUlvONmOZtfzgFz
   ByTB10QgxOR0TqBQejhRNzOfkVJ+5YiUqrXF5Y5lKh/DeuNcP5yLWGsEbtLiOSp
   a/TPg7JpJHxyendzWTBfX0cxOnKPjgBzi4diinWGdkF8kjdfnycQZXZeYGejmJl
   ZeGl9i2icVqaNVailT6F5iJ90m6mvuTS4OK05M0vDk0Q4XUtwvKOzrcd3iq9uis
   F81M1OIcR7lEewwcLp7tuNNkM3uNna3F2JQFo97Vriy/Xl4/f1cf5VWzXyym7PH
   hhx4dbgYKAAA7"
   ALT="Larry">
 
I'm modifying these 3 lines essentially:

print "Content_Type: image/png\n\n";
@pngdata = <OUT>;
print @pngdata;

to look like:

print "<img src=\"data:image/png;base64,",@pngdata,\">";

But I get screen garbage (eg text representing the image):

IT looks like:

<img src="data:image/png;base64,‰PNG

   
IHDR  €  à

Any ideas where I am going wrong?

Cheers,
Max

Author

Commented:
The program in full (which doesn't work) currently is:

#!/usr/bin/perl
use IPC::Open2;

#($returned,$ifdtable)=split('::::',`/web/cgi-bin/hydro/has/ifd_table_simple 23000 123000`);
#print $returned;
print "Content_Type: text/html\n\n";
print "<html>";
print "<head><title>testing 1 2 3<\/title><\/head>";
print "<body>";
print "blah blah blah";

$process = open2(OUT, GP, "/usr/bin/gnuplot");
print GP << "end_commands";
set term png
set title "Test 2"
plot sin(x)
end_commands
close GP;
$| = 1;
#print "Content_Type: image/png\n\n";
@pngdata = <OUT>;
#print @pngdata;
print "<img src=\"data:image/png;base64,",@pngdata,\">";

print "blah blah blah again";
print "<\/body>";
print "<\/html>";

Author

Commented:
I tried the sample from the site in a file, and that doesnt display a picture either:

<html>
<head><title>test</title></head>
<body>

testing

<IMG

SRC="data:image/gif;base64,R0lGODdhMAAwAPAAAAAAAP///ywAAAAAMAAwAAAC8IyPqcvt3wCcDkiLc7C0qwyGHhSWpjQu5yqmCYsapyuvUUlvONmOZtfz

gFzByTB10QgxOR0TqBQejhRNzOfkVJ+5YiUqrXF5Y5lKh/DeuNcP5yLWGsEbtLiOSpa/TPg7JpJHxyendzWTBfX0cxOnKPjgBzi4diinWGdkF8kjdfnycQZXZeY

GejmJlZeGl9i2icVqaNVailT6F5iJ90m6mvuTS4OK05M0vDk0Q4XUtwvKOzrcd3iq9uisF81M1OIcR7lEewwcLp7tuNNkM3uNna3F2JQFo97Vriy/Xl4/f1cf5V

WzXyym7PHhhx4dbgYKAAA7"ALT="Larry">

testing 2

</body>
</html>

Author

Commented:
Some more googling uncovered this statement:

" It seems that IE6 does not support the RFC 2397 'data' scheme. Maybe it will
in IE7.
 Firefox and Opera support it. "

I tried the sample and it worked in mozilla, and doesnt work on my IE6. Still didnt get my own program working in mozilla but defeats the purpose if it won't work on IE6 and below. It isn't an acceptable solution to only work in version 7 of IE and above.

Cheers,
Max


ozo
CERTIFIED EXPERT
Most Valuable Expert 2014
Top Expert 2015

Commented:
Could you make one script generate the
Content_Type: text/html\n\n
<html>
<head><title>testing 1 2 3<\/title><\/head>
<body>
blah blah blah
<img src="otherscripttogenerateimage?datatogenerateimage">
and do the
Content_Type: image/png\n\n@pngdata
in the otherscript

Commented:
What if you put the file handle into binary mode before you print the image?

Author

Commented:
Ozo:

 RE: <img src="otherscripttogenerateimage?datatogenerateimage">

Is this valid HTML, can I call a script like that? Also will it too be part of the same or similar RFC that won't support less than I.E. 7?

Mjcoyne:

Are you suggesting this line:
$process = open2(OUT, GP, "/usr/bin/gnuplot");
be given a binary mode for the open ?

How will this help, what are you trying to achieve there?
Sorry I don't understand where you are going with that.


p.s. Apologies for not getting back sooner, I have been on a course this week.

Regards and thanks,
Max

Commented:
You earlier said "But I get screen garbage (eg text representing the image)".  This is usually the result of printing a binary file in ASCII mode.  I would put whatever filehandle produced the "screen garbage" into binary mode.

Why do you not want gnuplot to just create a file on the Linux server that you can link to via HTML?

I'm not sure how we got into base64...

Author

Commented:
I've decided I will go down the path of creating files on linux, and linking to it, and will need to run a file cleanup routine in cron.

Whilst I think the answers given will work under certain circumstances (eg certain browser levels) I don't feel it is suitable for me, so I will still award the points for the suggestions.

Cheers,
Max

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.