[PYTHON] Continuation: I tried to introduce the block diagram generation tool blockdiag

■ Introduction

Last time, I introduced the block diagram generation tool blockdiag to CentOS6.5.

This time, I will describe the tips when using it via a web server.

■ Premise

■ Overview

-If the blockdiag command is defined in the script, consider that it will be executed by the "apache" user / group.

-It is necessary to grant write permission for the apache user or group to the block diagram generation destination.

-When executing the blockdiag command, it is necessary to explicitly specify the font because it is executed by the apache user.

■ Output directory

From the default documentroot

# mkdir /var/www/html/data
# chown .apache /var/www/html/data

■ Sample script

# vi /var/www/html/index.php
--------------------
<html>
<head>
<meta charset="UTF-8">
</head>
<body>
<h1>BlockDiag Online</h1>
<form action='index.php' method='post'>
<textarea name='text' cols='120' rows='20'>
blockdiag admin {
   // Set M17N text using label property.
   A [label = "Ki"];
   B [label = "Accepted"];
   C [label = "Turn"];
   D [label = "Conclusion"];

   A -> B -> C -> D;

   // Use M17N text directly (need to quote).
spring->summer->autumn->winter;

   // Use M17N text including symbol characters (need to quote).
   "Spring is dawn" -> "summer=Night" -> "autumn.evening" -> "winter&be making an effort";
}
</textarea>
<hr />
<input type='submit' value='conversion' />
</form>

<?php
  if (isset($_POST['text'])){
    //Define path etc.
    $dir = "data/";
    $font = "/usr/share/fonts/ipa-gothic/ipag.ttf";
    $filename = sprintf("svg%04d",mt_rand(0,9999));
    $fd = sprintf("./{$dir}%s.diag",$filename);
    $ff = sprintf("./{$dir}%s.svg" ,$filename);
    $text = $_POST['text'];
    
    //Create block diagram
    file_put_contents($fd,$text);
    
    //blockdiag command execution
    $command = sprintf("blockdiag -f {$font} -Tsvg %s",$ft);
    $Err = `$command`;
    printf("command: %s<br />",$command);
    print "<hr />";
    
    if (file_exists($ff)) {
      echo "$ff exists<br />";
      printf("ff: %s<br />",$ff);
      print "<img src=\"{$ff}\" />";
    } else {
      echo "<span style='color:#ff0000;'>$ff does not exist. Seems to have failed to generate<br />";
      echo "If successful, the following SVG file will be generated</span><br />";
      print "<img src=\"/cp_root/nippon.svg\" />";
    }
  }
?>
</body>
</html>

Grant execution authority

# chmod 755 /var/www/html/index.php

■ Execute from a browser

http://example.com/index.php

20141008_blockdiag_sample01.png


▽ Conversion result

20141008_blockdiag_sample02.png

■ Check the generated file

# ls -l /var/www/html/data/svg5038.svg
-rw-r--r--1 apache apache 7465 October 8 20:04 2014 /var/www/html/data/svg5038.svg

I think that the content has become a little useful for actual usage.

That's it.

Recommended Posts

Continuation: I tried to introduce the block diagram generation tool blockdiag
I tried to introduce the block diagram generation tool blockdiag
I tried to introduce Pylint
I tried to move the ball
I tried to estimate the interval.
I tried to summarize the umask command
I tried to recognize the wake word
I tried the OSS visualization tool, superset
I tried to summarize the graphical modeling.
I tried to estimate the pi stochastically
I tried to touch the COTOHA API
I tried web scraping to analyze the lyrics.
I tried to optimize while drying the laundry
I tried to save the data with discord
I tried to touch the API of ebay
Qiita Job I tried to analyze the job offer
LeetCode I tried to summarize the simple ones
I tried to implement the traveling salesman problem
I tried to predict the price of ETF
I tried to vectorize the lyrics of Hinatazaka46!
I tried to learn the sin function with chainer
I tried to graph the packages installed in Python
When I tried to introduce python3 to atom, I got stuck
I tried to detect the iris from the camera image
I tried to summarize the basic form of GPLVM
I tried to touch the CSV file with Python
I tried to predict the J-League match (data analysis)
Continuation ・ I tried to make Slackbot after studying Python3
I tried to approximate the sin function using chainer
I tried to put pytest into the actual battle
[Python] I tried to graph the top 10 eyeshadow rankings
I tried to visualize the spacha information of VTuber
I tried to paste
I tried to erase the negative part of Meros
I tried to solve the problem with Python Vol.1
I tried to simulate the dollar cost averaging method
I tried to redo the non-negative matrix factorization (NMF)
I tried to identify the language using CNN + Melspectogram
I tried to notify the honeypot report on LINE
I tried to complement the knowledge graph using OpenKE
I tried to classify the voices of voice actors
I tried to draw a configuration diagram using Diagrams
[Python] I tried to summarize the array, dictionary generation method, loop method, list comprehension notation
I tried to compress the image using machine learning
I tried to summarize the string operations of Python
I made a tool to automatically generate a simple ER diagram from the CREATE TABLE statement
I tried to find the entropy of the image with python
I tried to find out the outline about Big Gorilla
I tried porting the code written for TensorFlow to Theano
[Horse Racing] I tried to quantify the strength of racehorses
I tried to simulate how the infection spreads with Python
I tried to get the location information of Odakyu Bus
I tried to find the average of the sequence with TensorFlow
I tried to notify the train delay information with LINE Notify
I tried to summarize the code often used in Pandas
I tried to display the time and today's weather w
I tried to summarize the commands often used in business
I tried to implement the mail sending function in Python
[TF] I tried to visualize the learning result using Tensorboard
[Machine learning] I tried to summarize the theory of Adaboost
I tried to enumerate the differences between java and python